April 15, 2012

Check if a YouTube video has been removed using PHP?

Question by user1334448

Possible Duplicate:
Check if Youtube and Vimeo-clips are valid

I have a website which links to a lot of YouTube videos but a lot of the time these videos are removed by either the user or YouTube. This means my site has a lot of dead videos on it and there are so many it would be a nightmare to check them all manually.

So I am looking for a way to check if the YouTube video is still valid using PHP. I have seen a lot of threads with people asking the same question but I can’t get any of the posted solutions to work.

Answer by Starx

Send request to this URL, to verify the existence of a video

http://gdata.youtube.com/feeds/api/videos/<videoid>

Here is an usage of it.

$headers = get_headers('http://gdata.youtube.com/feeds/api/videos/' . $videoId);
if (!strpos($headers[0], '200')) {
    echo "The YouTube video you entered does not exist";
    return false;
}
April 9, 2012

How to make youtube embed code HTML valid for w3c validator?

Question by Shree

Here is my code.

<div>
    <object>
        <param name="movie" value="http://www.youtube.com/v/Cbspv1ZKR8o?version=3&amp;hl=en_US" />
        <param name="allowFullScreen" value="true" />
        <param value="transparent" name="wmode" />
        <param name="allowscriptaccess" value="always" />
        <embed src="http://www.youtube.com/v/Cbspv1ZKR8o?version=3&amp;hl=en_US" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" class="cssVideo"></embed>
    </object>
</div>

But the above code failed to pass the W3C Validator.
I search and try to follow these guides but I am unable to solve the problem.

Can anyone help me with this? Thanks.

Answer by Starx

Actually, you don’t need to always use the embeded code provided from youtube.

Below is a typical way to change the embedded version of the youtube and validate it as well.

<object type="application/x-shockwave-flash" width="425" height="350" data="http://www.youtube.com/v/n">
     <param name="movie" value="http://www.youtube.com/v/n" />
     <a href="http://get.adobe.com/flashplayer/" target="blank"><img src="http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif" alt="Get flash player to play to this file" width="88" height="31" /></a><br />
</object>

Where n is the youtube id of the video.

See a demo validation result

Cheers Prabhu 🙂

March 7, 2012

RegEx pattern to get the YouTube video ID from any YouTube URL

Question by Shackrock

Let’s take these URLs as an example:

  1. http://www.youtube.com/watch?v=8GqqjVXhfMU&feature=youtube_gdata_player
  2. http://www.youtube.com/watch?v=8GqqjVXhfMU

This PHP function will NOT properly obtain the ID in case 1, but will in case 2. Case 1 is very common, where ANYTHING can come behind the YouTube ID.

/**
 * get YouTube video ID from URL
 *
 * @param string $url
 * @return string YouTube video id or FALSE if none found. 
 */
function youtube_id_from_url($url) {
    $pattern = 
        '%^# Match any YouTube URL
        (?:https?://)?  # Optional scheme. Either http or https
        (?:www.)?      # Optional www subdomain
        (?:             # Group host alternatives
          youtu.be/    # Either youtu.be,
        | youtube.com  # or youtube.com
          (?:           # Group path alternatives
            /embed/     # Either /embed/
          | /v/         # or /v/
          | /watch?v=  # or /watch?v=
          )             # End path alternatives.
        )               # End host alternatives.
        ([w-]{10,12})  # Allow 10-12 for 11 char YouTube id.
        $%x'
        ;
    $result = preg_match($pattern, $url, $matches);
    if (false !== $result) {
        return $matches[1];
    }
    return false;
}

What I’m thinking is that there must be a way where I can just look for the “v=”, no matter where it lies in the URL, and take the characters after that. In this manner, no complex RegEx will be needed. Is this off base? Any ideas for starting points?

Answer by Starx

Instead of regex. I hightly recommend parse_url() and parse_str():

$url = "http://www.youtube.com/watch?v=8GqqjVXhfMU&feature=youtube_gdata_player";
parse_str(parse_url( $url, PHP_URL_QUERY ), $vars );
echo $vars['v'];    

Done

...

Please fill the form - I will response as fast as I can!