2

I have developed a website where users can add and embed YouTube videos. I store the YouTube ID in a database.

I am facing an issue where videos are being removed by YouTube, generally down to copyright/DMCA, private videos or users deleting them.

I would like to run an hourly CRON job that checks if these videos still exist using a PHP script but I can't find any resources on this.

Could anybody help?

Peter

Cristiana Chavez
  • 11,349
  • 5
  • 55
  • 54
Peter Stuart
  • 2,362
  • 7
  • 42
  • 73
  • 1
    possible duplicate of [How do I check if a video exists on YouTube, using PHP?](http://stackoverflow.com/questions/1383073/how-do-i-check-if-a-video-exists-on-youtube-using-php) – cmorrissey May 27 '15 at 18:49
  • Try downloading url data, and check for words like "not found" or "invalid url", if you cloudn't find any direct solution – Alex May 27 '15 at 18:49
  • This wouldn't be suitable because video titles might contain these words and YouTube could change their wording in a moment without warning. I would like a solid method – Peter Stuart May 27 '15 at 18:54

1 Answers1

1

Better solution:

if (sizeof($videoResponse['items'])) {
    // Video exist, do stuff     
}

Source: verify if video exist with youtube api v3

You can check if your videoResponse is an object:

$videoResponse = $youtube->videos->listVideos('snippet,statistics', array(
    'id' => $videoId 
));    

if (is_object($videoResponse['items'][0])) {
    // Video exist, do stuff     
}

Or check if it is !empty:

if (!empty($videoResponse['items'][0])) {
    // Video exist, do stuff     
}

You can actually check for privacy status too:

$videoResponse = $youtube->videos->listVideos('status', array(
    'id' => $videoId 
));    

$privacyStatus = $videoResponse['items'][0]['status']['privacyStatus'];
Community
  • 1
  • 1
Mark1ra
  • 28
  • 5