Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/275.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用youtube API获取403错误_Php_Json_Youtube_Httpwebrequest - Fatal编程技术网

Php 使用youtube API获取403错误

Php 使用youtube API获取403错误,php,json,youtube,httpwebrequest,Php,Json,Youtube,Httpwebrequest,我正在我的频道上播放最新上传的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&a

我正在我的频道上播放最新上传的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返回的):


所以我的问题是,如果resonse是403,如何处理(检测)呢?这样我就可以做别的事了

您无法使用
文件获取内容
读取HTTP头。我会使用类似卷曲的东西:

function get_youtube_content($url) 
{
    if(!function_exists('curl_init'))
    { 
        die('CURL is not installed!');
    }

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $output = curl_exec($ch);
    $http_status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);

    return ($http_status == '403') ? false : $output;
}
您可以在函数前面添加“@”,以防止出现PHP错误消息:
@file\u获取内容

在if条件下使用此选项:

<?php
if($url = @file_get_contents("http://something/")) {
    // success code and json_decode, etc. here
} else {
    // error message here
}
?>


我觉得添加此方法大大增加了加载时间!或者可能是其他原因……不管怎样,我输入了这段代码,并将检查是否很快再次出现错误。@sign只告诉PHP忽略错误消息。它不能防止错误的发生。
<?php
if($url = @file_get_contents("http://something/")) {
    // success code and json_decode, etc. here
} else {
    // error message here
}
?>