Reputation: 385
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.
Upvotes: 8
Views: 16122
Reputation: 2972
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
}
Upvotes: 37
Reputation: 3463
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');
};
Upvotes: 13
Reputation: 4134
Here is my solution. Before you can make requests to the API, you must generate server key from google developers console.
$youtubeApiKey = 'YOUR_KEY';
function checkVideoWorking($videoID) {
global $youtubeApiKey;
$url = sprintf("https://www.googleapis.com/youtube/v3/videos?part=id&id=%s&key=%s", $videoID, $youtubeApiKey);
$response = file_get_contents($url);
$working = true;
$decoded = json_decode($response, true);
if ($decoded['pageInfo'] && $decoded['pageInfo']['totalResults'] == 0) {
$working = false;
}
return $working;
}
Upvotes: 0
Reputation: 385
I forgot to put it. This is the solution i found and think it is the best way to do it as google like to control all. You have to create a public key in the google console, like that google knows that it is your site which ask him to verify a video and he can has well charge you some of your quotas.
I do what follows and verify the header code :
$file_headers = @get_headers('https://www.googleapis.com/youtube/v3/videos?part=id&id='. $id_video .'&key='.$publicKey);
if ($file_headers[0] == 'HTTP/1.0 200 OK') {
$datavideo = file_get_contents('https://www.googleapis.com/youtube/v3/videos?part=id&id='. $id_video .'&key='. $publicKey);
$data = json_decode($datavideo);
if ($data->items)
$this->idvideo = $id_video;
else
...
} else {
...
}
Hope it helps like that i will hae reputation :D
Upvotes: -1
Reputation: 607
Did you find a solution?
The official way is to use videos.list and submit the video id. The documentation mentions the following error, but it's never thrown:
"notFound (404) videoNotFound The video that you are trying to retrieve cannot be found. Check the value of the request's id parameter to ensure that it is correct."
https://developers.google.com/youtube/v3/docs/videos/list?hl=de
Upvotes: 0