How do I check if a video exists on YouTube, using PHP?
Here is a solution that doesn't involve using youtube api, it checks if the video id exists when the url is loaded
function checkYoutubeUrlIsValid($url) {
$buffer = file_get_contents($url);
$matches = [];
preg_match('#[a-zA-Z0-9_-]{11}$#', $url, $matches);
return strpos($buffer, $matches[0]) !== false;
}
Hope that helps
What about using Youtube's API?
After all, that would mean using some official, which is less likely to change than going with parsing some HTML page.
For more information: YouTube APIs and Tools - Developer's Guide: PHP
The Retrieving a specific video entry seems quite interesting: if you send a request to an URL like this one:
http://gdata.youtube.com/feeds/api/videos/videoID
(replacing "videoID" by the ID of the video, of course – "GeppLPQtihA" in your example), you'll get some ATOM feed if the video is valid; and "Invalid id" if it's not
And, I insist: this way, you rely on a documented API, and not on some kind of behavior that exists today, but is not guaranteed.
As commented by @dbro, the answer by Pascal MARTIN was an acceptable answer at that time. However, since the API had moved forward, fixed and improved, a new solution that works is the following. Please take note that this is based on the technique provided by @Pascal and I quote:
...if you send a request to an URL like this one
http://gdata.youtube.com/feeds/api/videos/videoID
(Replacing "videoID" by the idea of the video, of course -- "GeppLPQtihA" in your example)
You'll get some ATOM feed (**STOP HERE**)
The new URL to use and this is for V3 of the API is https://www.googleapis.com/youtube/v3/videos?id={the_id_of_the_video}&key={your_api_key}&part={parts}
WHERE
{the_id_of_the_video}
you should know what this is
{your_api_key}
is your app's key that can be found in your Developer Console
{parts}
a comma-separated list, check here for valid valuesIf the Video Id is VALID you will get data in the items field that includes the Id of the video and the information you queried through the parts parameter.
If the Video Id is NOT VALID then you will get an empty items.
Supplying a wrong key gives you an ERROR 400 (an error object).
/**
* Check youtube url, check video exists or not,
*
* @param $url full youtube video url
*
* @return string - yotube video id
*/
public static function checkYoutube($url)
{
if (preg_match('%(?:youtube(?:-nocookie)?\.com/(?:[^/]+/.+/|(?:v|e(?:mbed)?)/|.*[?&]v=)|youtu\.be/)([^"&?/ ]{11})%i', $url, $match))
{
$headers = get_headers('http://gdata.youtube.com/feeds/api/videos/' . $match[1]);
if (strpos($headers[0], '200'))
{
return $match[1];
}
return false;
}
return false;
}
link:
https://github.com/DimitriMikadze/php-helpers
Here is the solution that I use to check if YouTube video exists using video id. This is C# code, but basically you can check if the thumbnail of the video exists, you will either get 200 or 404 which is very convenient.
private async Task<bool> VideoExists(string id)
{
var httpClient = new HttpClient();
var video = await httpClient.GetAsync($"https://img.youtube.com/vi/{id}/0.jpg");
return video.IsSuccessStatusCode;
}
Youtube has support for the oEmbed format.
Compared to the xml responsed provided by Pascal MARTIN, mine has only to download 600 bytes against 3800 bytes, making it faster and less bandwidth cosuming (only 1/6 of the size).
function yt_exists($videoID) {
$theURL = "http://www.youtube.com/oembed?url=http://www.youtube.com/watch?v=$videoID&format=json";
$headers = get_headers($theURL);
return (substr($headers[0], 9, 3) !== "404");
}
$id = 'yyDUC1LUXSU'; //Video id goes here
if (yt_exists($id)) {
// Yep, video is still up and running :)
} else {
// These aren't the droids you're looking for :(
}