Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

verify if video exist with youtube api v3

I'm trying to verify if a youtube video (with the id_video) is valid/exist , using the youtube api V3. That's what i do (y2oy7b4SFgE is the id of the video i test):

$file_headers = @get_headers('https://www.googleapis.com/youtube/v3/videos?part=id&id=y2oy7b4SFgE&key=ma_clé_api_publique');
   //exit(var_dump($file_headers));
if ($file_headers[0] == 'HTTP/1.0 200 OK') {
  $resultat = $file_headers[0].'Goood'
} else {
  $resultat = $file_headers[0].'PasGoood'
}

But i have a "code": 403, "message": "There is a per-IP or per-Referer restriction configured on your API key and the request does not match these restrictions. Please use the Google Developers Console to update your API key configuration if request from this IP or referer should be allowed."

Its working well when no referer. But when i put one, i tried with the name of my website or with the ip of my vps server, each time it doesn'work.

like image 879
user3417911 Avatar asked Mar 20 '15 12:03

user3417911


People also ask

Is YouTube data API v3 free?

Yes, using the YouTube API does not incur any monetary cost for the entity calling the API. If you go over your quota an 403 Error will be returned by the API.

Can you still use YouTube API v2?

What should I do? You can continue using the v2 API for comments and uploading video captions for now, and we'll be adding this functionality into the v3 API soon. While we don't have specific dates yet, we will release that functionality so that developers have as much time as possible to migrate to v3.


2 Answers

There are two ways I am aware of to check if a video exists using the YouTube video id.

The simplest is to use the oembed url for the video. This requires no authentication and returns a 404 header when the video is invalid. You should really be doing this with curl, as depending on your setup file_get_contents may not be an option for you...

$headers = get_headers('https://www.youtube.com/oembed?format=json&url=http://www.youtube.com/watch?v=' . $videoID);

if(is_array($headers) ? preg_match('/^HTTP\\/\\d+\\.\\d+\\s+2\\d\\d\\s+.*$/',$headers[0]) : false){
    // video exists
} else {
    // video does not exist
}

The second is to use V3 of the data api. To use this method you will need to generate an api key in the developer console.

$response = file_get__contents('https://www.googleapis.com/youtube/v3/videos?part=id&id=' . $videoID . '&key=' . $apiPublicKey);
$json = json_decode($response);

if (sizeof($json['items'])) {
    // video exists
} else {
    // video does not exist
}
like image 105
Harry B Avatar answered Oct 13 '22 20:10

Harry B


In PHP it is very simple to verify if video exists. Use PHP function file_get_contents to check either video exists or not.

general Video URL to check a video would be
$video_url = https://www.youtube.com/oembed?format=json&url=http://www.youtube.com/watch?v=VideoId

$video_url = @file_get_contents('https://www.youtube.com/oembed?format=json&url=http://www.youtube.com/watch?v=Xu4MTv5wgys');
if($video_url) {
    echo('video exists');
} else {

    echo('video does not exists');

};
like image 37
Shahbaz Avatar answered Oct 13 '22 20:10

Shahbaz