I am showing the latest uploaded youtube videos on my channel. It works fine but from time to time it gives this error, and renders my entire site in error!
[phpBB Debug] PHP Warning: in file /my_youtube/functions.php on line 5:
file_get_contents(http://gdata.youtube.com/feeds/api/users/ElectronicsPubVideos/uploads?v=2&alt=json&max-results=5&orderby=published):
failed to open stream:
HTTP request failed! HTTP/1.0 403 Forbidden
I don't know what is the probem, might be a time to time error from youtube side? Anyway this is my function that parses the JSON file (If it actually get returned from youtube):
function GetLatestVideos()
{
$url = file_get_contents('http://gdata.youtube.com/feeds/api/users/ElectronicsPubVideos/uploads?v=2&alt=json&max-results=5&orderby=published');
$i = 0;
if($result = json_decode($url, true))
{
foreach($result['feed']['entry'] as $entry)
{
$vids[$i]["title"] = $entry['title']['$t'];
$vids[$i]["desc"] = $entry['media$group']['media$description']['$t'];
$vids[$i]["thumb"] = $entry['media$group']['media$thumbnail'][2]['url'];
$vids[$i]["url"] = $entry['link'][0]["href"];
$vids[$i]["id"] = $entry['media$group']['yt$videoid']['$t'];
$i++;
}
return $vids;
}
else return "";
}
So my question is, how to handle (detect) if resonse is 403? so that I can do something else!