Check if a YouTube video has been removed using PHP?

php, youtube

Solution

Use youtube V3 API to check if the video is accessible or not. For further info refer to the following doc

https://developers.google.com/youtube/v3/docs/videos/list

Basically, make a GET request using your API key

GET https://www.googleapis.com/youtube/v3/videos?part=id&id={VIDEO_ID}&key={YOUR_API_KEY}

You will receive a sample response like:

{
 "kind": "youtube#videoListResponse",
 "etag": "\"sZ5p5Mo8dPpfIzLYQBF8QIQJym0/N6KWrfNRyfPOarHfhvNS7j4jfxM\"",
 "pageInfo": {
  "totalResults": 0,
  "resultsPerPage": 0
 },
 "items": []
}*

If you get an empty array in `items` of response, the video is not accessible.

The following says about V2 of the API, which is now deprecated. (https://developers.google.com/youtube/2.0/developers_guide_protocol_video_entries).

Note: The YouTube Data API (v2) has been officially deprecated as of March 4, 2014. Please refer to our deprecation policy for more information. Please use the YouTube Data API (v3) for new integrations and migrate applications still using the v2 API to the v3 API as well.

Send request to this URL, to verify the existence of a video

http://gdata.youtube.com/feeds/api/videos/<videoid>

Here is an usage of it.

$headers = get_headers('http://gdata.youtube.com/feeds/api/videos/' . $videoId);
if (!strpos($headers[0], '200')) {
    echo "The YouTube video you entered does not exist";
    return false;
}

Problem

Possible Duplicate: Check if Youtube and Vimeo-clips are valid I have a website which links to a lot of YouTube videos but a lot of the time these videos are removed by either the user or YouTube. This means my site has a lot of dead videos on it and there are so many it would be a nightmare to check them all manually. So I am looking for a way to check if the YouTube video is still valid using PHP. I have seen a lot of threads with people asking the same question but I can't get any of the posted solutions to work.

Original source

Related problems