verify if video exist with youtube api v3
api, php, video, youtube, youtube-api
Solution
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
}
Problem
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.