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.
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.
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.
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
}
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');
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With