无法打开流:HTTP请求失败!HTTP/1.0 400错误请求-试图获取YouTube API数据


Failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request - Trying To Get YouTube API Data

我最近一直在尝试使用YouTube的分析API获取一些数据。我在一个域上遇到了问题,但在另一个域中没有任何错误。

除了从index.php改为api.php的action="之外,它使用的是完全相同的代码-我只是不明白为什么它在一个域上工作,而在另一个域不工作?!

准确的错误是:

警告:file_get_contents(http://gdata.youtube.com/feeds/api/users/):无法打开流:HTTP请求失败!第22行/customers/2/c/9/catalyst.yt/httpd.www/api.php中的HTTP/1.0 400错误请求警告:file_get_contents(http://gdata.youtube.com/feeds/api/users/?alt=json):无法打开流:HTTP请求失败!HTTP/1.0 400错误请求,位于/customers/2/c/9/catalyst.yt/httpd.www/api.php的第24行

这是代码

<html>
<head>
    <title>Get YouTube Channel Data</title>
</head>
<body>
<form action="api.php" method="GET">
    <input type="text" name="username" />
    <input type="submit" value="Submit!" />
</form>
</body>
</html>
<?php 
$channelUser = $_GET['username'];

$data = file_get_contents('http://gdata.youtube.com/feeds/api/users/' . $channelUser);
$data = file_get_contents('http://gdata.youtube.com/feeds/api/users/' . $channelUser . '?alt=json');
$data = json_decode($data, true);
$stats_data = $data['entry']['yt$statistics'];
$img_data = $data['entry']['media$thumbnail'];
echo $channelUser . ' Has <strong>'.$stats_data['subscriberCount'].'</strong> Subscribers.<br />';
echo $channelUser . ' Has <strong>'.$stats_data['totalUploadViews'].'</strong> Total Views.<br />';
echo 'Logo: <br /><img src="' .$img_data["url"].'" /><br />';

?>

编辑

下面是一个带有cURL的v3 api请求的示例。因此,您需要在开发人员控制台中创建一个api密钥。如果您还没有服务器密钥,请创建一个新项目,选择它,然后单击"凭据"(在"API&auth"中)->创建新密钥->服务器密钥->创建(如果您在本地计算机上开发,则无需输入任何内容)。

然后用API键替换代码中的占位符。

$api_key = 'YOUR_API_KEY';
$url = 'https://www.googleapis.com/youtube/v3/channels?part=snippet,statistics&key=' . $api_key . '&forUsername=' . $_GET['username'];
// initializes the request
$curl = curl_init();
// sets the url
curl_setopt($curl, CURLOPT_URL, $url);
// enables that curl_exec() returns content instead of status code
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
// allows redirects
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
// checks if a secure site has been requested
if(preg_match('/^https:'/'//', $url)){
    // if so, verification is disabled
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
}
// performs the actual request
$data = curl_exec($curl);
// destructs the request
curl_close($curl);
// this line converts the json string which is returned into a php object
$data = json_decode($data);
// you can access your stats like this:
var_dump($data->items[0]->statistics->subscriberCount);
var_dump($data->items[0]->statistics->videoCount);
var_dump($data->items[0]->snippet->thumbnails->default->url);

请注意,在您的最终产品中不建议禁用ssl验证,只是现在更容易。稍后,您应该正确验证证书(在你问之前,我从来没有这样做过)

我希望这对你有用。


原始答案

看起来php.ini中禁用了allow_url_fopen。如果php.ini中有一行allow_url_fopen = Off,请将其更改为allow_url_fopen = On

但我更喜欢使用cURL:

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, 'http://gdata.youtube.com/feeds/api/users/' . $channelUser);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
$data = curl_exec($curl);
curl_close($curl);

curl扩展也必须启用:extension = php_curl.dll(可能有一个类似的行以分号开头,您可以删除它)。但在大多数配置中,这已经完成了。