我正在我的频道上显示最新上传的 youtube 视频。它工作正常,但有时会出现此错误,并使我的整个网站出错!
[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
我不知道问题是什么,可能是来自 youtube 方面的不时错误?无论如何,这是我解析 JSON 文件的函数(如果它实际上是从 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 "";
}
所以我的问题是,如果 resonse 是 403,如何处理(检测)?这样我就可以做其他事情了!