سؤال

I've attempted to use the YouTube service API V3 and have run into an issue I'm unsure of how to proceed.

I'm calling my script through Jquery's Ajax. Nothing crazy. Passing in an id for the video I want and away I go.

The script is as such:

session_start();
$return_msg = array();

// ===================================
//  Require Google libraries
// ===================================
set_include_path("../../includes/");
if(!(@require_once('Google/Client.php')))
{
    $return_msg['error'] = 'Unable to includes Google Client Library:<br>'.$e->getMessage();
    return $return_msg;
}
if(!(@require_once('Google/Service/YouTube.php')))
{
    $return_msg['error'] = 'Unable to includes Google YouTube Library:<br>'.$e->getMessage();
    return $return_msg;
}

// ===================================
//  Include Global settings
// ===================================
if(!(@require_once('Global-Settings.php')))
{
    $return_msg['error'] = 'Unable to includes site settings:<br>'.$e->getMessage();
    return $return_msg;
}
$globals = new Globals();

// Set API key
$api_key = $globals->youtube_key;

if(!isset($data['id']))
{
    $return_msg['error'] = 'Unable to determine the video you are attempting to find.';
    return $return_msg;
}

// ===================================
//  Create Client
// ===================================
try
{
    $client = new Google_Client();
    $client->setApplicationName("YouTube_Test");
    $client->setDeveloperKey($api_key);
}catch(Exception $e){
    $return_msg['error'] = 'Error creating Google client:<br>'.$e->getMessage();
    return $return_msg;
}   

// ===================================
//  Get video
// ===================================
try
{
    $service = new Google_Service_YouTube($client);
    $response = $service->videos->listVideos('id,snippet,contentDetails', array(
        "id" => $data['id']
    ));

    $return_msg['results'] = $response;

    foreach($response->items as $video)
    {
        $item = array();
        $item['id']      = $video['id'];
        $item['snippet'] = $video['snippet'];]
        $item['contentDetails'] = $video['contentDetails'];
        $return_msg['items'][$item['id']] = $item;
    }

}catch(Exception $e){
    $return_msg['error'] = 'Error finding video:<br>'.$e->getMessage();
    return $return_msg;
}

return $return_msg;

The reason I am storing the individual items in an array is because line $return_msg['results'] = $response; returns fields, yet not the items object as I'd expect.

Anywho, once I return all this to JS and log it to console, I see that inside the 'snippets' array there are no thumbnails.

Not sure why, or what I'm doing incorrectly as far as that goes. I've tried to log as much info as I could and I simply can't find any trace of the thumbnails collection.

هل كانت مفيدة؟

المحلول

A simple way to get thumbnail of a video from youtube is by using the video id:

http://img.youtube.com/vi/<your_video_id>/0.jpg

[0.jpg, 1.jpg, 2.jpg and 3.jpg can be used]

For Eg:

http://img.youtube.com/vi/-w8KI3A5zu4/0.jpg

if you got the video_id from youtube API, you can simply use this to get the image.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top