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
  • 689
  • 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
  • 856
  • 4
  • 20
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
  • 10,968
  • 6
  • 26
  • 48