2

I want to check if entered URL exist on YouTube or not before saving that URL in my database

I am using this code

$url = 'https://www.youtube.com/watch?v=KeaoSIKaxeg';
if(preg_match('/http:\/\/www\.youtube\.com\/watch\?v=[^&]+/', $url)) 
{
    dd('match');
}
else 
{
    dd('not match');
}

I have tried everything but nothing works. It always returns 'not match' even if the URL is valid.

Arun J
  • 687
  • 4
  • 14
  • 27

2 Answers2

2
$headers = get_headers('http://www.youtube.com/oembed?url=https://www.youtube.com/watch?v=KeaoSIKaxeg');
if (!strpos($headers[0], '200')) {
    echo "The YouTube video you entered does not exist";
}
iAmGroot
  • 950
  • 1
  • 6
  • 22
1

You're trying to match against http://, not https://:

$url = 'https://www.youtube.com/watch?v=KeaoSIKaxeg';
if(preg_match('/https:\/\/www\.youtube\.com\/watch\?v=[^&]+/', $url)) {
     dd('match');
}
else {
    dd('not match');
}
symlink
  • 11,984
  • 7
  • 29
  • 50