Conor Reid
Conor Reid

Reputation: 95

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

I've recently been trying to get some data with YouTube's analytics API. I'm getting a problem on ONE domain but it's working without any errors on another.

It's using the EXACT same code apart from the form action="" which was changed from index.php to api.php - I just can't figure out why it's working on one domain and isn't on the other?!

The exact error is:

Warning: file_get_contents(http://gdata.youtube.com/feeds/api/users/): failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request in /customers/2/c/9/catalyst.yt/httpd.www/api.php on line 22 Warning: file_get_contents(http://gdata.youtube.com/feeds/api/users/?alt=json): failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request in /customers/2/c/9/catalyst.yt/httpd.www/api.php on line 24

Here's the code

<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 />';


?>

Upvotes: 2

Views: 5174

Answers (1)

paolo
paolo

Reputation: 2538

EDIT

Here's an example for a v3 api request with cURL. Therefore you need to create an api key in the developer console. If you don't already have a server key, create a new project, select it and then click 'Credentials' (in 'APIs & auth) -> create new key -> server key -> create (you don't have to enter anything if you develop on a local machine).

Then replace the placeholder in the code with your API key.

$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);

Note that disabling the ssl verification is not recommended in your final product, it's just easier for now. Later, you should properly verify the certificate. (Before you ask, i've never done this)

I hope this works for you.


Original Answer

Looks like allow_url_fopen is disabled in php.ini. If there's a line allow_url_fopen = Off in your php.ini, change it to allow_url_fopen = On.

But I'd prefer using 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);

The curl extension has to be enabled, too: extension = php_curl.dll (There could be a similar line starting with a semicolon which you can just remove). But on most configurations, this is already done.

Upvotes: 1

Related Questions