How do I check if a video exists on YouTube, using PHP?
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 :(
}
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.
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;
}
Request the URLs with the HEAD method, like so:
HEAD /watch?v=p72I7g-RXpg HTTP/1.1
Host: www.youtube.com
HTTP/1.1 200 OK
[SNIP]
HEAD /watch?v=p72I7g-BOGUS HTTP/1.1
Host: www.youtube.com
HTTP/1.1 303 See Other
[SNIP]
Location: http://www.youtube.com/index?ytsession=pXHSDn5Mgc78t2_s7AwyMvu_Tvxn6szTJFAbsYz8KifV-OP20gt7FShXtE4gNYS9Cb7Eh55SgoeFznYK616MmFrT3Cecfu8BcNJ7cs8B6YPddHQSQFT7fSIXFHd5FmQBk299p9_YFCrEBBwTgtYhzKL-jYKPp2zZaACNnDkeZxCr9JEoNEDXyqLvgbB1w8zgOjJacI4iIS6_QvIdmdmLXz7EhBSl92O-qHOG9Rf1HNux_xrcB_xCAz3P3_KbryeQk_9JSRFgCWWgfwWMM3SjrE74-vkSDm5jVRE3ZlUI6bHLgVb7rcIPcg
A new way to get a YouTube video data after September 2021, is by cURL in PHP:
function getYouTubeData($videoId) {
$theURL = "https://www.youtube.com/oembed?url=http://www.youtube.com/watch?v=$videoId&format=json";
$curl = curl_init($theURL);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$body = curl_exec($curl);
curl_close($curl);
return json_decode($body, true);
}
Usage:
$ytData = getYouTubeData($video_id);
if (empty($ytData)) {
$error = 'YouTube movie data could not be fetched.';
}
$title = $ytData['title'];
Sample output:
Array
(
[title] => Use online tools available at Laminas Starter Kit - Laminas MVC
[author_name] => Divix
[author_url] => https://www.youtube.com/channel/UC6lBQpNdQH6cu0j15qhkCAg
[type] => video
[height] => 113
[width] => 200
[version] => 1.0
[provider_name] => YouTube
[provider_url] => https://www.youtube.com/
[thumbnail_height] => 360
[thumbnail_width] => 480
[thumbnail_url] => https://i.ytimg.com/vi/LjDdAcB9-Mo/hqdefault.jpg
[html] => <iframe width="200" height="113" src="https://www.youtube.com/embed/LjDdAcB9-Mo?feature=oembed" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With