Below is an example code and a functions in php to check if youtube video is still valid:
<?php
/**
* Check Valid YouTube ID: Check_YT_ID, Check_Youtube_Video functions
*
* PHP version 5.3+
*
* Licensed under The MIT License.
* Redistribution of these files must retain the above copyright notice.
*
* @author Emilian Robert Vicol <byrev[*]yahoo.com>
* @copyright Copyright 2020+ Emilian Robert Vicol
* @license https://opensource.org/licenses/mit-license.html
* @link https://robertvicol.com/
* @version 1.0
*/
# ~~~~ Function to Check only ID
function Check_Youtube_ID($id) {
$url = "https://i.ytimg.com/vi/" . $id . "/mqdefault.jpg";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_exec($ch);
$response_code = (int)curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
return ($response_code == 200);
}
# ~~~~ Function to Check Youtube URL or ID
function Check_Youtube_Video($yturl) {
if (strlen($yturl) < 12) { return Check_Youtube_ID($yturl); }
$preg1 = '@youtube.com\/watch\?v=(.*?)$@';
if ( preg_match($preg1, $yturl, $match) ) {
$id = explode("&",$match[1]);
return Check_Youtube_ID($id[0]);
}
}
$id = 'F7TsM5x1idg';
if (Check_Youtube_ID($id))
echo "TRUE,";
else
echo "FALSE,";
if (Check_Youtube_Video("https://youtube.com/watch?v=F7TsM5x1idg"))
echo "TRUE,";
else
echo "FALSE,";
if (Check_Youtube_Video($id))
echo "TRUE,";
else
echo "FALSE,";
$yturl posible format/sintax:
- https://www.youtube.com/watch?v=[ID]&feature=…&…
- https://www.youtube.com/watch?v=[ID]
- www.youtube.com/watch?v=[ID]
- youtube.com/watch?v=[ID]
- https://www.youtube.com/v/[ID]
- www.youtube.com/v/[ID]
- youtube.com/v/[ID]
- [ID]
- …. and any valid youtube video url
Be First to Comment