Home » Php » php – verify if video exist with youtube api v3

php – verify if video exist with youtube api v3

Posted by: admin July 12, 2020 Leave a comment


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');
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.

How to&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


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');



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.”



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;


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 {

Hope it helps like that i will hae reputation 😀