Jump to content

How do I check the statuses of the YouTube videos in my database?


Fluoresce

Recommended Posts

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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");

?>
  • Like 1
Link to comment
Share on other sites

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);
}
?>
  • Like 1
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.