Check if YouTube video has been deleted using PHP?

Possible duplicate:
Check the correctness of Youtube and Vimeo-clips

I have a website that links to many YouTube videos, but many times when these videos are deleted either by the user or YouTube. This means that there are a lot of dead videos on my site, and so many would be a nightmare to check all of them manually.

So, I'm looking for a way to check if a YouTube video really works with PHP. I have seen many threads with people asking the same question, but I cannot get any published solutions to work.

+3
source share
4 answers

API- youtube V3 .

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

, GET API

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

, :

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

items, .


V2 API, . (https://developers.google.com/youtube/2.0/developers_guide_protocol_video_entries).

. API YouTube (v2) 4 2014 . . . API YouTube (v3) , API v2, API v3.

URL-,

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

.

$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;
}
+8

http://gdata.youtube.com/feeds/api/videos/$id

, ,

, :

if (file_get_contents('http://gdata.youtube.com/feeds/api/videos/'.$videoId) == 'Video not found') {
  echo 'Video deleted';
}
+5

Youtube HTTP 404, . , . URL-.

, 200.

, PHP Curl URL-, , . Curl :

$http_status = curl_getinfo( $handle, CURLINFO_HTTP_CODE );

You can save the last_checked timestamp with each URL in the database, and then run cronjob every N minutes to check a small number of URLs, and update the timestamps when you are done. Be sure to save the status code in the database.

Then you can deal with non 200 status codes as you like in your code when displaying the url.

0
source

All Articles