php Get all Video information of a User in Youtube
Due to work needs, I obtained all the video information of a user on Youtube, wrote this class, and shared it with everyone.
YTUserVideo.class.php
<?php /** Get all Video information of a User in Youtube * Date: 2015-01-08 * Author: fdipzone * View: 1.0 * * Func: * public getVideosInfo Get all the video information of the user * private getVideoNum Get the number of user videos * private getVideoInfo Get video information * private getContent video introduction * private unescape unicode to Chinese */ class YTUserVideo{ // class start private $_user = ''; // username /** Initialize * @param String $user username */ public function __construct($user=''){ if($user!=''){ $this->_user = $user; }else{ throw new Exception("user is empty", 1); } } /** Get all video information of user * @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; } /** Get 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 information * @param String $index the sequence 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']; // 中图 $info['big_photo'] = $ytarr['iurl']; // 大图 $info['title'] = $ytarr['title']; // title $info['content'] = $videoContent; // Introduction $info['publish_date'] = $videoPublish; // publish time $info['length_seconds'] = $ytarr['length_seconds']; // video length (s) $info['view_count'] = $ytarr['view_count']; // view count $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) == "&#") { $ar[$k] = iconv("UCS-2BE","UTF-8",pack("n",substr($v,2,-1))); } } return join("",$ar); } } // class end ?>
demo.php
<?php require 'YTUserVideo.class.php'; $obj = new YTUserVideo('GOtriphk'); // Username GOtriphk https://www.youtube.com/user/GOtriphk/videos $videosInfo = $obj->getVideosInfo(); echo '<pre>'; print_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] => [Bika Chao ssss raids Tsim Tsui! ! ! 】Hong Kong Bika Super Exhibition [content] => There is a Bika Super Exhibition in Hong Kong, and the world's largest "Bika Super 3D Spaceship" with a height of 13 meters will be exhibited at the same venue. There will be 700 sprites with different characters. Of course, they are not too small. Jinduo’s limited-time souvenirs can be grabbed first, and the Christmas version of Bika, who made a special trip from Yokohama to Hong Kong, is in full contact with fans. In short, it’s fun to fly or not! The ONE x Pokemon Christmas Dream Flight Date: November 9, 2014 to January 4, 2015 Time: 10am-10pm Venue: The ONE UG2 Atrium [publish_date] => 1415257662 [length_seconds] => 124 [view_count] => 603 [avg_rating] => 0.0 [embed] => //www.youtube.com/embed/jYDwFozp6PY ) .....
Source code download address: click to view