So, I have this basic code. It pretty much checks a user input to see if it is a Youtube URL. If it is, then it grabs the Youtube ID. After getting the Youtube ID it then checks to see if the video is still active.
The problem is with the current code, I am either getting one or two outputs:
The link provided is not a working Youtube link. Please try again.
OR
The video is currently active!
I believe the problem is when trying to use function checkYoutubeOnline and then checking the value. Does anyone have any helpful advice or would like to give feedback or tips? I appreciate the time to look over my code! Thanks a bunch.
$youtubeid = youtube_id_from_url($link); //Gets Youtube ID from URL.
if ($youtubeid != false){ //If was able to retrieve ID continue
checkYoutubeOnline($youtubeid); //Check to see if Youtube link is active
if ($youtubeid = true){ //Check result from function
echo 'The video is currently active!';
}else{
echo 'This video is no longer active.';
}
}
}else{
echo 'The link provided is not a working Youtube link. Please try again.';
}
The functions are
function youtube_id_from_url($url) {
$pattern =
'%^# Match any youtube URL
(?:https?://)? # Optional scheme. Either http or https
(?:www\.)? # Optional www subdomain
(?: # Group host alternatives
youtu\.be/ # Either youtu.be,
| youtube\.com # or youtube.com
(?: # Group path alternatives
/embed/ # Either /embed/
| /v/ # or /v/
| /watch\?v= # or /watch\?v=
) # End path alternatives.
) # End host alternatives.
([\w-]{10,12}) # Allow 10-12 for 11 char youtube id.
$%x'
;
$result = preg_match($pattern, $url, $matches);
if ($result) {
return $matches[1];
}else{
return false;
}
}
function checkYoutubeOnline($youtubeid) {
if (!$data = @file_get_contents("http://gdata.youtube.com/feeds/api/videos/".$youtubeid)) return false;
if ($data == "Video not found") return false;
return true;
}