PHP Classes

File: example_get_video.php

Recommend this page to a friend!
  Classes of Manuel Lemos   PHP YouTube API Get Video Info   example_get_video.php   Download  
File: example_get_video.php
Role: Example script
Content type: text/plain
Description: YouTube get video example
Class: PHP YouTube API Get Video Info
Get details and manage videos using YouTube API v3
Author: By
Last change:
Date: 2 years ago
Size: 1,659 bytes
 

Contents

Class file image Download
<?php
/*
 * example_get_video.php
 *
 * @(#) $Id: example_get_video.php,v 1.1 2016/10/08 08:37:01 mlemos Exp $
 *
 */

   
require('youtube_client.php');
   
   
$youtube = new youtube_client_class;
   
$youtube->key = ''; $application_line = __LINE__;
   
    if(
strlen($youtube->key) === 0)
    {
        die(
'Please go to Google Developer Console, get a key for the '.
           
'YouTube API and set it in the line '.$application_line);
    }

   
$parameters = array(
       
'id'=>'3Xw3617p06M'
   
);
   
$success = $youtube->ListVideos($parameters, $details);
?>
<!DOCTYPE html>
<html>
<head>
<title>Example of getting a video from YouTube</title>
</head>
<body>
<h1>Example of getting a video from YouTube</h1>
<?php
       
if($success)
        {
           
$items = $details['items'];
            if(
count($items) === 0)
            {
                echo
'<h2>No videos were returned.</h2>', "\n";
            }
            else
            {
               
$item = $items[0];
               
$snippet = $item['snippet'];
               
$content = $item['contentDetails'];
                echo
'<h2>Returned videos: '. count($items), '</h2>', "\n";
                echo
'<h3>First video:</h3>', "\n";
                echo
'<p>Title: <a href="https://www.youtube.com/watch?v='.HtmlSpecialChars($parameters['id']).'">', HtmlSpecialChars($snippet['title']), '</a></p>', "\n";
                echo
'<p>Description: ', nl2br(HtmlSpecialChars($snippet['description'])), '</p>', "\n";
                echo
'<p>Duration: ', $youtube->ConvertDuration($content['duration']), ' seconds</p>', "\n";
                echo
'<p>Thumbnail: <img src="', HtmlSpecialChars($snippet['thumbnails']['standard']['url']), '"></p>', "\n";
            }
            echo
'<pre>', HtmlSpecialChars(print_r($details, 1)), "\n", '</pre>';
        }
        else
            echo
'Error: ', HtmlSpecialChars($youtube->error), "\n";
?>
</body>
</html>