How to get all YouTube user video information via PHP

Source: Internet
Author: User
PHP gets all youtube user video information

Get all the video information from a user on YouTube, write this class, and share it with you for your job needs.

YTUserVideo.class.php

<?php/** get a YouTube user all video information * date:2015-01-08* author:fdipzone* ver:1.0** func:* public getvide OsInfo Get user All video information * Private getvideonum Get user video number * Private Getvideoinfo get video info * Private getcontent video profile * pri Vate unescape Unicode to Chinese */class ytuservideo{//class start private $_user = ';//user name/** Initialize * @param St        Ring $user User Name */Public Function __construct ($user = ') {if ($user! = ') {$this->_user = $user;        }else{throw new Exception ("User is Empty", 1);        }}/** get user all video information * @return Array */Public Function Getvideosinfo () {$info = array ();        Get the number of videos $videonum = $this->getvideonum ();            Get video information for ($i =1; $i <= $videonum; $i + +) {$videoInfo = $this->getvideoinfo ($i);        Array_push ($info, $videoInfo);    } return $info;     }/** gets the number of user videos * @return int */Private Function getvideonum () {   $videos = simplexml_load_file (' http://gdata.youtube.com/feeds/base/users/'. $this->_user. '        /uploads?max-results=1&start-index=1 ');        $videonum = $videos->children (' OpenSearch ', true)->totalresults;    return $videonum;        /** Get Video Info * @param String $index The serial number of the video @return Array */Private Function Getvideoinfo ($index) { Get video ID and introduction $video = Simplexml_load_file (' http://gdata.youtube.com/feeds/base/users/'. $this->_user. '        /uploads?max-results=1&start-index= '. $index);        $videoId = Str_replace (' http://gdata.youtube.com/feeds/base/videos/', ', (String) ($video->entry->id));        $videoContent = $this->getcontent ($video->entry->content);        $videoPublish = Strtotime ($video->entry->published);        Get video information based on video id $content = file_get_contents (' http://youtube.com/get_video_info?video_id= '. $videoId);        Parse_str ($content, $ytarr);        $info = Array ();       $info [' id '] = $videoId; $info [' thumb_photo '] = $ytarr [' Thumbnail_url '];             Thumbnail $info [' middle_photo '] = $ytarr [' iurlmq '];                  In Figure $info [' big_photo '] = $ytarr [' Iurl '];                     Big picture $info [' title '] = $ytarr [' title '];                     Title $info [' content '] = $videoContent;                Introduction $info [' publish_date '] = $videoPublish;   Release time $info [' length_seconds '] = $ytarr [' Length_seconds '];           Video Length (s) $info [' view_count '] = $ytarr [' View_count '];           Views $info [' avg_rating '] = $ytarr [' avg_rating ']; Average rating $info [' embed '] = '//www.youtube.com/embed/'. $videoId;    Embed return $info;        /** Get Video Introduction * @param string $content content * @return String */Private Function getcontent ($content) { Preg_match ('/<span> (. *?)        <\/span>/is ', $content, $matches);    return $this->unescape ($matches [1]); }/* Unicode to Chinese * @param String $str Unicode String * @return String */Private Function unescape ($str) {$str = Rawurldecode ($STR);        Preg_match_all ("/(?:%u.{4}) |& #x. {4};|&#\d+;|.+/u", $str, $r);        $ar = $r [0]; foreach ($ar as $k + $v) {if (substr ($v, 0,2) = = "%u") {$ar [$k] = Iconv ("Ucs-2be", "UTF-8", Pack ("            H4 ", substr ($v,-4)));            }elseif (substr ($v, 0,3) = = "& #x") {$ar [$k] = Iconv ("Ucs-2be", "UTF-8", Pack ("H4", substr ($v, 3,-1)));            }elseif (substr ($v, 0,2) = = "the") {$ar [$k] = Iconv ("Ucs-2be", "UTF-8", Pack ("n", substr ($v, 2,-1)));    }} return join ("", $ar); }}//Class end?>


demo.php

<?phprequire ' YTUserVideo.class.php '; $obj = new Ytuservideo (' gotriphk '); User name Gotriphk Https://www.youtube.com/user/gotriphk/videos$videosinfo = $obj->getvideosinfo (); Echo ' <pre > ';p rint_r ($videosInfo); Echo ' </pre> ';? >


Output:

Array (    [0] = = Array        (            [id] = Jydwfozp6py            [Thumb_photo] = http://i.ytimg.com/vi/ Jydwfozp6py/default.jpg            [Middle_photo] = Http://i.ytimg.com/vi/jYDwFozp6PY/mqdefault.jpg            [Big_photo] = = Http://i.ytimg.com/vi/jYDwFozp6PY/hqdefault.jpg            [title] = "than Casue SSSS!!!" "Hong Kong than Casue exhibition            [content] + Hong Kong Casue exhibition, the same venue on the world's largest, 13 meters high" than the Casue vertical flying boat ", there will be 700 of the same role of the fine spirit of the present, of course, but also small and unmotivated multi-period of limited-time products can be the first hog and from the promenade to the port of the Christmas version than Casue with the full touch of the fan, the total flying is like playing! The one x pet little soul Dreaming to fly Date: November 9, 2014 to January 4, 2015:10am-10pm: The one UG2 atrium            [publish_date] and 1415257662            [ Length_seconds] = 124            [View_count] = 603            [avg_rating] = 0.0            [Embed] =//www.youtube.com /embed/jydwfozp6py) .....        

This article explains how to get a YouTube user all video information through PHP, and more about the PHP Chinese web.

Related recommendations:

PHP character encoding conversion class related content

Explanation of the performance of PHP Implode/explode, serialize, JSON, Msgpack

About PHP Flock usage examples

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.