Jump to content

Extract information from YouTube into a string


defroster

Recommended Posts

Give this a try.

 

<?php
function video_entry($url) {
  if (strchr($url, 'http://')) {
    $video_id = parse_url($url, PHP_URL_QUERY);
    $video_id = str_replace('v=', '', $video_id);
  }
  else {
    $video_id = $url;
  }
  
  // Make the initial API request
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, 'http://gdata.youtube.com/feeds/api/videos/' . $video_id);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  // Store XML results in a string
  $result = curl_exec($ch);
  curl_close($ch);
  
  if ($xml = simplexml_load_string($result)) {
    return $xml;
  }
  else {
    die ('An error occurred trying to load XML data.');
  }
}
$video = video_entry('http://www.youtube.com/watch?v=kMAI26FPeyM');
echo $video->title; // Scooter Power!!
?>

 

You can use either the video id, or the entire URL.

 

e.g.

 

$video = video_entry('kMAI26FPeyM');
// or
$video = video_entry('http://www.youtube.com/watch?v=kMAI26FPeyM');

 

Also if you use print_r on $video, it will give you all of the array and object information returned.

 

E.g.

$video = video_entry('http://www.youtube.com/watch?v=kMAI26FPeyM');
echo '<pre>';
print_r($video);
echo '</pre>';

 

will return:

 

SimpleXMLElement Object
(
    [id] => http://gdata.youtube.com/feeds/api/videos/kMAI26FPeyM
    [published] => 2010-08-22T08:24:02.000Z
    [updated] => 2010-08-23T09:15:57.000Z
    [category] => Array
        (
            [0] => SimpleXMLElement Object
                (
                    [@attributes] => Array
                        (
                            [scheme] => http://schemas.google.com/g/2005#kind
                            [term] => http://gdata.youtube.com/schemas/2007#video
                        )

                )

            [1] => SimpleXMLElement Object
                (
                    [@attributes] => Array
                        (
                            [scheme] => http://gdata.youtube.com/schemas/2007/categories.cat
                            [term] => Entertainment
                            [label] => Entertainment
                        )

                )

            [2] => SimpleXMLElement Object
                (
                    [@attributes] => Array
                        (
                            [scheme] => http://gdata.youtube.com/schemas/2007/keywords.cat
                            [term] => scooter
                        )

                )

            [3] => SimpleXMLElement Object
                (
                    [@attributes] => Array
                        (
                            [scheme] => http://gdata.youtube.com/schemas/2007/keywords.cat
                            [term] => power
                        )

                )

        )

    [title] => Scooter Power!!
    [content] => sp
    [link] => Array
        (
            [0] => SimpleXMLElement Object
                (
                    [@attributes] => Array
                        (
                            [rel] => alternate
                            [type] => text/html
                            [href] => http://www.youtube.com/watch?v=kMAI26FPeyM&feature=youtube_gdata
                        )

                )

            [1] => SimpleXMLElement Object
                (
                    [@attributes] => Array
                        (
                            [rel] => http://gdata.youtube.com/schemas/2007#video.responses
                            [type] => application/atom+xml
                            [href] => http://gdata.youtube.com/feeds/api/videos/kMAI26FPeyM/responses
                        )

                )

            [2] => SimpleXMLElement Object
                (
                    [@attributes] => Array
                        (
                            [rel] => http://gdata.youtube.com/schemas/2007#video.related
                            [type] => application/atom+xml
                            [href] => http://gdata.youtube.com/feeds/api/videos/kMAI26FPeyM/related
                        )

                )

            [3] => SimpleXMLElement Object
                (
                    [@attributes] => Array
                        (
                            [rel] => http://gdata.youtube.com/schemas/2007#mobile
                            [type] => text/html
                            [href] => http://m.youtube.com/details?v=kMAI26FPeyM
                        )

                )

            [4] => SimpleXMLElement Object
                (
                    [@attributes] => Array
                        (
                            [rel] => self
                            [type] => application/atom+xml
                            [href] => http://gdata.youtube.com/feeds/api/videos/kMAI26FPeyM
                        )

                )

        )

    [author] => SimpleXMLElement Object
        (
            [name] => josdoming
            [uri] => http://gdata.youtube.com/feeds/api/users/josdoming
        )

)

Wow that is brilliant! Thank you so much!!

 

I managed to extract

-title

-description

-date published

 

Is there a possibility to also get the votes, youtube username, and the tags like (scooter, power) from the clip?

I was trying but was unable to get those.

 

Thanks SO MUCH for the help!

 

The code currently using:

<?php
function video_entry($url) {
  if (strchr($url, 'http://')) {
    $video_id = parse_url($url, PHP_URL_QUERY);
    $video_id = str_replace('v=', '', $video_id);
  }
  else {
    $video_id = $url;
  }
  
  // Make the initial API request
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, 'http://gdata.youtube.com/feeds/api/videos/' . $video_id);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  // Store XML results in a string
  $result = curl_exec($ch);
  curl_close($ch);
  
  if ($xml = simplexml_load_string($result)) {
    return $xml;
  }
  else {
    die ('An error occurred trying to load XML data.');
  }
}
$video = video_entry('kMAI26FPeyM');
#$video = video_entry('http://www.youtube.com/watch?v=kMAI26FPeyM');
echo $video->title; // Video title
echo $video->content; // Description
echo $video->published; // date


?>

 

 

 

No idea how to get votes, but this gets the tags and the authors name:

 

<?php
function video_entry($url) {
  if (strchr($url, 'http://')) {
    $video_id = parse_url($url, PHP_URL_QUERY);
    $video_id = str_replace('v=', '', $video_id);
  }
  else {
    $video_id = $url;
  }
  
  // Make the initial API request
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, 'http://gdata.youtube.com/feeds/api/videos/' . $video_id);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 1);
  curl_setopt($ch, CURLOPT_PROXY, 'http://emeacache.uk.oracle.com:80');
  // Store XML results in a string
  $result = curl_exec($ch);
  curl_close($ch);
  
  if ($xml = simplexml_load_string($result)) {
    $count = 0;
    foreach ($xml->category as $category) {
      if ($count > 1) {
        $xml->tags[] = $category[0]['term'];
      }
      $count++;
    }
    
    return $xml;
  }
  else {
    die ('An error occurred trying to load XML data.');
  }
}
$video = video_entry('http://www.youtube.com/watch?v=kMAI26FPeyM');
echo $video->title; // Video title
echo $video->content; // Description
echo $video->published; // date
echo $video->tags[0] . ' - ' . $video->tags[1]; // scooter - power
echo $video->author->name; // author name
?>

Thanks Wolphie. But unfortunately when I use the code you provided I get an error?

An error occurred trying to load XML data.

 

code used:

<?php
function video_entry($url) {
  if (strchr($url, 'http://')) {
    $video_id = parse_url($url, PHP_URL_QUERY);
    $video_id = str_replace('v=', '', $video_id);
  }
  else {
    $video_id = $url;
  }
  
  // Make the initial API request
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, 'http://gdata.youtube.com/feeds/api/videos/' . $video_id);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 1);
  curl_setopt($ch, CURLOPT_PROXY, 'http://emeacache.uk.oracle.com:80');
  // Store XML results in a string
  $result = curl_exec($ch);
  curl_close($ch);
  
  if ($xml = simplexml_load_string($result)) {
    $count = 0;
    foreach ($xml->category as $category) {
      if ($count > 1) {
        $xml->tags[] = $category[0]['term'];
      }
      $count++;
    }
    
    return $xml;
  }
  else {
    die ('An error occurred trying to load XML data.');
  }
}
$video = video_entry('http://www.youtube.com/watch?v=kMAI26FPeyM');
echo $video->title; // Video title
echo $video->content; // Description
echo $video->published; // date
echo $video->tags[0] . ' - ' . $video->tags[1]; // scooter - power
echo $video->author->name; // author name
?>

Thanks Wolphie. But unfortunately when I use the code you provided I get an error?

An error occurred trying to load XML data.

 

code used:

<?php
function video_entry($url) {
  if (strchr($url, 'http://')) {
    $video_id = parse_url($url, PHP_URL_QUERY);
    $video_id = str_replace('v=', '', $video_id);
  }
  else {
    $video_id = $url;
  }
  
  // Make the initial API request
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, 'http://gdata.youtube.com/feeds/api/videos/' . $video_id);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 1);
  curl_setopt($ch, CURLOPT_PROXY, 'http://emeacache.uk.oracle.com:80');
  // Store XML results in a string
  $result = curl_exec($ch);
  curl_close($ch);
  
  if ($xml = simplexml_load_string($result)) {
    $count = 0;
    foreach ($xml->category as $category) {
      if ($count > 1) {
        $xml->tags[] = $category[0]['term'];
      }
      $count++;
    }
    
    return $xml;
  }
  else {
    die ('An error occurred trying to load XML data.');
  }
}
$video = video_entry('http://www.youtube.com/watch?v=kMAI26FPeyM');
echo $video->title; // Video title
echo $video->content; // Description
echo $video->published; // date
echo $video->tags[0] . ' - ' . $video->tags[1]; // scooter - power
echo $video->author->name; // author name
?>

 

I'm still completely stuck trying to figure out why there is a XML error? any ideas? Thanks

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.