I am using this code to get related videos. With this code I get title and link of the video. How can I get the thumb of the video?
<?php
$JSON = file_get_contents("http://gdata.youtube.com/feeds/api/videos/FYpunY-gXxU/related?v=2&alt=json");
$JSON_Data = json_decode($JSON);
$title = $JSON_Data->{'feed'}->{'entry'};
for ($i = 1; $i < 25; $i++)
{
echo ($title[$i]->{'title'}->{'$t'}) . "<br />";
echo $title[$i]->{'link'}[0]->{'href'} . "<br /><br />";
}
?>
When using the json_decode
, the best way is to print the entire result for yourself and then see the necessary data you have to get...
Do this:
`<?php
$JSON = file_get_contents("http://gdata.youtube.com/feeds/api/videos/FYpunY-gXxU/related?v=2&alt=json");
$JSON_Data = json_decode($JSON);
echo '<pre>';
print_r($JSON_Data);
?>`
You'll get to know what exactly you have to extract... I leave that to you... :)
I would :-
<?php
$json = file_get_contents("http://gdata.youtube.com/feeds/api/videos/FYpunY-gXxU/related?v=2&alt=json");
$json_data = json_decode($json, true);
foreach((array)$json_data['feed']['entry'] as $video){
echo $video['title']['$t'].'<br/>';
echo $video['link'][0]['href'].'<br/>';
?>
<img src="<?php echo $video['media$group']['media$thumbnail'][0]['url']?>" />
<img src="<?php echo $video['media$group']['media$thumbnail'][1]['url']?>" />
<?php
}
?>