2

Below code, I got title and description but I want image as well as how to get the main image of video from JSON data.

$videoid = 'cMC_PtgKDJE';
$apikey = 'xyzxyzxyz';  //My API key

$json = file_get_contents('https://www.googleapis.com/youtube/v3/videos?id='.$videoid.'&key='.$apikey.'&part=snippet');
$ytdata = json_decode($json);
print_r($ytdata);
echo '<h1>Title: ' . $ytdata->items[0]->snippet->title . '</h1>';
echo 'Description: ' . $ytdata->items[0]->snippet->description;

2 Answers 2

4

Each YouTube video has 4 generated images.You can create the url as fallows

https://img.youtube.com/vi/<video-id-here>/0.jpg

DEMO

If you want high quality image then you can read following atricle

how to get a youtube video’s thumbnail image in high quality

How to get video id from youtube url

$url = "http://www.youtube.com/watch?v=7zWKm-LZWm4&feature=relate";
parse_str( parse_url( $url, PHP_URL_QUERY ), $url_vars );
echo $url_vars['v'];    
  // Output: 7zWKm-LZWm4
?> 
Sign up to request clarification or add additional context in comments.

3 Comments

thanks a lot now i got it one more thing i want to ask in this case i had given youtube id directly in varaible from one video but how to get youtube video id at first using php.
actually i copied from youtube url is there any way to extract it from whole url that's what i want to ask. like youtube.com/watch?v=CmBm59V-SOE
Hi, I see you're new to SO. If you feel an answer solved the problem, please mark it as 'accepted' by clicking the green check mark. This helps keep the focus on older SO which still don't have answers
1

Each video on YouTube provides several thumbnails of different quality

Default (for small thumbnails): $ytdata->items[0]->snippet->thumbnails->default->url;

Medium: $ytdata->items[0]->snippet->thumbnails->medium->url;

High: $ytdata->items[0]->snippet->thumbnails->high->url;

Standard: $ytdata->items[0]->snippet->thumbnails->standard->url;

Maximum resolution: $ytdata->items[0]->snippet->thumbnails->maxres->url;

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.