Claud
Claud

Reputation: 997

YouTube Data API failed to open stream: HTTP request failed! HTTP/1.0 403 Forbidden

Have set up an API KEY, added my domain to allowable referrers, and try to make a call to the YouTube V3 API using PHP as follows:

file_get_contents("https://www.googleapis.com/youtube/v3/search?part=snippet&q=my_search_query&type=video&key=my_application_key") 

But

failed to open stream: HTTP request failed! HTTP/1.0 403 Forbidden

Have I missed something obvious?

Upvotes: 0

Views: 3748

Answers (2)

Liam Allan
Liam Allan

Reputation: 1115

is file_get_contents supported on your web server? many web servers dont support this function due to security risks. have you looked into using curl instead?

maybe something like this:

$option = array(
        'part' => 'snippet', 
        'q' => 'search_query',
        'type' => 'video',
        'key' => 'your_key'
    );
$url = "https://www.googleapis.com/youtube/v3/search?".http_build_query($option, 'a', '&');
$curl = curl_init($url);

curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);  
curl_setopt($curl, CURLOPT_HTTPHEADER, false);

$json_response = curl_exec($curl);

curl_close($curl);

$responseObj = json_decode($json_response);

Upvotes: 0

Claud
Claud

Reputation: 997

Transpires there's multiple different API keys one can generate. The default is a 'browser api key' but the one I needed for PHP running on my server (of course) was a 'server api key'.

I set that up, and whitelisted my server's IP.

Problem solved.

Upvotes: 2

Related Questions