Fluoresce Posted September 17, 2014 Share Posted September 17, 2014 I embed YouTube videos on my site. The problem is, the videos often get deleted by the user, which means that the videos obviously become unavailable on my site. This happens often, which means that I regularly have to look through my site, find unavailable videos and delete them. I am looking for an automated way of doing this. Here's what I've got so far: <?php $con = mysql_connect("", "", ""); mysql_select_db(""); $qry = "SELECT video_id FROM `table`"; $run = mysql_query($qry, $con); while($row = mysql_fetch_assoc($run)) { $headers = get_headers("http://gdata.youtube.com/feeds/api/videos/{$row['video_id']}"); if(!strpos($headers[0], '200')) { echo "Unavailable: {$row['video_id']}<br />"; return false; } else { echo "Available: {$row['video_id']}<br />"; } } ?> The code's very slow and doesn't work. It produces a list of available videos and then stops when it finds one that is unavailable. The problem is, the video that it says is unavailable is usually available, which means that it's getting it wrong. Is there a faster, more accurate way of doing it? Basically, I want a fast and accurate way of checking the availability of the thousands of videos in my database and I want to produce a list of the ones that are unavailable. Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted September 17, 2014 Share Posted September 17, 2014 Use cURL with a multi handle and make HEAD requests to only fetch the headers. The reason why get_headers() is slow is because it only makes one request at a time and fetches the entire document (for whatever reason). 1 Quote Link to comment Share on other sites More sharing options...
Fluoresce Posted September 17, 2014 Author Share Posted September 17, 2014 Hi, Jacques1. I will be very grateful if you could elaborate a bit more. I've never heard of cURL. I shall start reading up now. Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted September 17, 2014 Share Posted September 17, 2014 cURL is a very popular client for all kinds of protocols (including HTTP). It's well suited for making HTTP requests with PHP, and it's much more powerful than built-in functions like get_headers(). For example, cURL can send multiple requests in parallel so that your script doesn't waste time by waiting for the responses. This massively increases performance. Quote Link to comment Share on other sites More sharing options...
QuickOldCar Posted September 18, 2014 Share Posted September 18, 2014 Youtube has opengraph and oembed data can look at. Youtube always returns a 200 response, they change the content on the page. If the title is "Youtube" it's dead. You can preg_match the opengraph for title Use urls similar to these for json or xml results for oembed http://www.youtube.com/oembed?url=http%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DfnpNFvcJS2k&format=json http://www.youtube.com/oembed?url=http%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DfnpNFvcJS2k&format=xml Can spend a few days each method and cleaning youtube urls to all be standard or just grabbing the id's. Here is a simple function to show a message or a video using json oembed <?php function checkDeadYoutube($url) { if(!$url || trim($url) == '') { echo "No url inserted"; exit(); } $oembed_json = "http://www.youtube.com/oembed?url=" . urlencode($url) . "&format=json"; $json = json_decode(@file_get_contents($oembed_json)); if (!$json || $json == null) { echo "Unable to connect"; exit(); } // echo '<pre>'.print_r($json,true).'</pre>'; $title = trim($json->title); $video = $json->html; if ($title != "Youtube") { echo $video; } else { echo "Dead Video"; } } // usage checkDeadYoutube("http://www.youtube.com/watch?v=fnpNFvcJS2k"); ?> 1 Quote Link to comment Share on other sites More sharing options...
QuickOldCar Posted September 18, 2014 Share Posted September 18, 2014 I noticed doing id's, missed that. Wanted to say it's most likely youtubes api limits is why seeing them unavailable. If you have many of them it's better to chunk the data and do so many at a time, mark them somehow in the database. Try this out, will show what ones are dead by id and json responses <?php function checkDeadYoutube($yt_id) { if(!$yt_id || trim($yt_id) == '') { echo "<a href='http://www.youtube.com/watch?v=".$yt_id."'target='_blank'>No url inserted</a><br />"; } $oembed_json = "http://www.youtube.com/oembed?url=http%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3D" . $yt_id . "&format=json"; $json = json_decode(@file_get_contents($oembed_json)); if (!$json || $json == null) { echo $yt_id." - <a href='http://www.youtube.com/watch?v=".$yt_id."'target='_blank'>Unable to connect</a><br />"; } // echo '<pre>'.print_r($json,true).'</pre>'; $title = trim($json->title); if ($title == "Youtube") { echo $yt_id." - <a href='http://www.youtube.com/watch?v=".$yt_id."'target='_blank'>".$title."</a><br />"; } } //example array $youtube_ids = array("XW99sBf4BUI","MkXVM6ad9nI","baaad","ou2Qk2D_Y4w","hX_yRbpCNVo","73e-tCWydFE","lytzFvAfRRk"); foreach($youtube_ids as $id){ checkDeadYoutube($id); } ?> 1 Quote Link to comment Share on other sites More sharing options...
Fluoresce Posted September 18, 2014 Author Share Posted September 18, 2014 Thanks for your help QuickOldCar and Jacques1. Jacques1, I have PMed you. Quote Link to comment Share on other sites More sharing options...
Fluoresce Posted September 21, 2014 Author Share Posted September 21, 2014 QuickOldCar, I need to speak to you in private, but it says that I can't send you a PM. Have you got too many messages or something? Quote Link to comment Share on other sites More sharing options...
QuickOldCar Posted September 21, 2014 Share Posted September 21, 2014 My mailbox was full, I cleared it. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.