Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New Coder
    Join Date
    Feb 2009
    Location
    Texas
    Posts
    56
    Thanks
    6
    Thanked 3 Times in 3 Posts

    Youtube Video Information from URL

    This snippet not all of it is mine, however it is published freely for use on the internet (I will try and find the websites). I put this together using Google's youtube API with PHP.

    At the bottom are pretty self explanatory variables that include the information. Uses GET so, a request could be youtube.php?url=http://youtube.com/?v=whatever&?=somethingblahblahblah

    PHP Code:
    // function to parse a video <entry>
        
    function parseVideoEntry($entry) {      
          
    $obj= new stdClass;
          
          
    // get nodes in media: namespace for media information
          
    $media $entry->children('http://search.yahoo.com/mrss/');
          
    $obj->title $media->group->title;
          
    $obj->description $media->group->description;
          
          
    // get video player URL
          
    $attrs $media->group->player->attributes();
          
    $obj->watchURL $attrs['url']; 
          
          
    // get video thumbnail
          
    $attrs $media->group->thumbnail[0]->attributes();
          
    $obj->thumbnailURL $attrs['url']; 
                
          
    // get <yt:duration> node for video length
          
    $yt $media->children('http://gdata.youtube.com/schemas/2007');
          
    $attrs $yt->duration->attributes();
          
    $obj->length $attrs['seconds']; 
          
          
    // get <yt:stats> node for viewer statistics
          
    $yt $entry->children('http://gdata.youtube.com/schemas/2007');
          
    $attrs $yt->statistics->attributes();
          
    $obj->viewCount $attrs['viewCount']; 
          
          
    // get <gd:rating> node for video ratings
          
    $gd $entry->children('http://schemas.google.com/g/2005'); 
          if (
    $gd->rating) { 
            
    $attrs $gd->rating->attributes();
            
    $obj->rating $attrs['average']; 
          } else {
            
    $obj->rating 0;         
          }
            
          
    // get <gd:comments> node for video comments
          
    $gd $entry->children('http://schemas.google.com/g/2005');
          if (
    $gd->comments->feedLink) { 
            
    $attrs $gd->comments->feedLink->attributes();
            
    $obj->commentsURL $attrs['href']; 
            
    $obj->commentsCount $attrs['countHint']; 
          }
          
          
    //Get the author
          
    $obj->author $entry->author->name;
          
    $obj->authorURL $entry->author->uri;
          
          
          
    // get feed URL for video responses
          
    $entry->registerXPathNamespace('feed''http://www.w3.org/2005/Atom');
          
    $nodeset $entry->xpath("feed:link[@rel='http://gdata.youtube.com/schemas/
          2007#video.responses']"
    ); 
          if (
    count($nodeset) > 0) {
            
    $obj->responsesURL $nodeset[0]['href'];      
          }
             
          
    // get feed URL for related videos
          
    $entry->registerXPathNamespace('feed''http://www.w3.org/2005/Atom');
          
    $nodeset $entry->xpath("feed:link[@rel='http://gdata.youtube.com/schemas/
          2007#video.related']"
    ); 
          if (
    count($nodeset) > 0) {
            
    $obj->relatedURL $nodeset[0]['href'];      
          }
        
          
    // return object to caller  
          
    return $obj;      
        }   

        
    // get video ID from $_GET 
        
    if (!isset($_POST['url'])) {
          die (
    'Please enter a youtube video URL');  
        } else {
            
    $vid stripslashes($_POST['url']);
            
    $string $vid;
            
    $url parse_url($string);
            
    parse_str($url['query']);
             
      
        }
        
        
    // set video data feed URL
        
    $feedURL 'http://gdata.youtube.com/feeds/api/videos/'$v;

        
    // read feed into SimpleXML object
        
    $entry simplexml_load_file($feedURL);
        
        
    // parse video entry
        
    $video parseVideoEntry($entry);


    //These variables include the video information
        
    $video_title mysql_real_escape_string(stripslashes($video->title));
        
    $video_description mysql_real_escape_string(stripslashes($video->description));
        
    $video_thumbnail stripslashes($video->thumbnailURL);
        
    $video_author mysql_real_escape_string(stripslashes($video->author));
        
    $embed '<object width="425" height="344"><param name="movie" value="http://www.youtube.com/v/'.$v.'&hl=en&fs=1"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/'.$v.'&hl=en&fs=1" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="344"></embed></object>'
    Free PSD to XHTML/CSS conversion - Dimby.net
    Which doctype should I use?

  • #2
    New to the CF scene
    Join Date
    Aug 2009
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks friend, i was searching for this.

  • #3
    New to the CF scene
    Join Date
    Sep 2009
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts
    great ~~!

    can you edit my code for me
    http://ebookict.com/test/

    code: http://ebookict.com/test.zip

  • #4
    Regular Coder dniwebdesign's Avatar
    Join Date
    Dec 2003
    Location
    Carrot River, Saskatchewan
    Posts
    841
    Thanks
    15
    Thanked 9 Times in 9 Posts
    I know this is older but it should be noted that this class will not work if allow_url_fopen is set to off.

    If there are any alternatives that do not use the allow_url_fopen, I would like to know.
    Dawson Irvine
    CEO - DNI Web Design
    http://www.dniwebdesign.com

  • #5
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Quote Originally Posted by dniwebdesign View Post
    I know this is older but it should be noted that this class will not work if allow_url_fopen is set to off.

    If there are any alternatives that do not use the allow_url_fopen, I would like to know.
    You can scrape a page using curl, and load the resulting string into simplexml. You can also set up a socket on 80 to read the source (socketing is almost always available).
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #6
    Regular Coder dniwebdesign's Avatar
    Join Date
    Dec 2003
    Location
    Carrot River, Saskatchewan
    Posts
    841
    Thanks
    15
    Thanked 9 Times in 9 Posts
    Actually, I have done the curl which was pretty simple. Almost everything remains the same except for one function (which I humbly donate to the community). ;-)

    PHP Code:
    function getVideo($url) {
            echo 
    $url;
            
    // get video ID from $_GET 
            
    if (!isset($url)) {
              return 
    'Please enter a YouTube video URL.';
            } else {
                
    $vid stripslashes($url);
                
    $string $vid;
                
    $url parse_url($string);
                
    parse_str($url['query']);
            }
            
    // set video data feed URL
            
    $feedURL 'http://gdata.youtube.com/feeds/api/videos/'$v;

            
            
    //Since allow_url_fopens is off  use curl to get
            //the xml document from youtube. ;)
            //Added by DNI Web Design, June 11, 2010
            
    $ch curl_init();    // initialize curl handle
            
    curl_setopt($chCURLOPT_URL,$feedURL); // set url to post to
            
    curl_setopt($chCURLOPT_RETURNTRANSFER,1); // return into a variable
            
    curl_setopt($chCURLOPT_TIMEOUT4); // times out after 4s
            
            
    $result curl_exec($ch); // run the whole process*/

            // read feed into SimpleXML object
            
    $entry simplexml_load_string($result);
            
            
    // parse video entry
            
    $video $this->parseVideoEntry($entry);
            
            
    //These variables include the video information
                
    $videos["title"] = stripslashes($video->title);
                
    $videos["description"] = stripslashes($video->description);
                
    $videos["thumbnail"] = stripslashes($video->thumbnailURL);
                
    $videos["author"] = stripslashes($video->author);
                
    $videos["embed"] = '<object width="425" height="344"><param name="movie" value="http://www.youtube.com/v/'.$v.'&hl=en&fs=1"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/'.$v.'&hl=en&fs=1" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="344"></embed></object>'
                
    $videos["key"] = $v;
                return 
    $videos;
        } 
    Dawson Irvine
    CEO - DNI Web Design
    http://www.dniwebdesign.com

  • #7
    New to the CF scene
    Join Date
    Apr 2008
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi dni, your code gives this error;

    PHP Code:
    Fatal errorUsing $this when not in object context in /home/downloa8/public_html/youtube-download/you.php  on line 32 

  • #8
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Drop the $this-> off the method call and treat both as functions. Or wrap both functions in a class and change it to object oriented.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •