: This article mainly introduces php to obtain all Video information of a User on Youtube. For more information about PHP tutorials, see. Php obtains all Video information of a User on Youtube.
This class is used to obtain all video information of a user on Youtube for work purposes and share it with you.
YTUserVideo. class. php
_ User = $ user;} else {throw new Exception ("user is empty", 1 );}} /** get all video information of the user * @ return Array */public function getVideosInfo () {$ info = array (); // get the number of videos $ videonum = $ this-> getVideoNum (); // get the 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 video sequence number * @ return Array */private function getVideoInfo ($ index) {// obtain the video id and description $ 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 ); // obtain the video information based on the 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']; // Large image $ info ['title'] = $ ytarr ['title']; // Title $ info ['content'] = $ videoContent; // overview $ info ['Publish _ date'] = $ ytarr ['timestamp']; // release date $ info ['length _ seconds '] = $ ytarr ['lengt H_seconds ']; // video length (s) $ info ['view _ count'] = $ ytarr ['view _ count']; // viewing times $ info ['avg _ rating '] = $ ytarr ['avg _ rating']; // average score $ info ['Embed '] =' // www.youtube.com/embed/'.w.videoid; // embed return $ info ;} /** get video introduction * @ param String $ content * @ return String */private function getContent ($ content) {preg_match ('/(. *?) <\/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,) = "% u") {$ ar [$ k] = iconv ("UCS-2BE", "UTF-8 ", pack ("H4", substr ($ v,-4);} elseif (substr ($ v,) = "& # x ") {$ ar [$ k] = iconv ("UCS-2BE", "UTF-8", pack ("H4", substr ($ v, 3,-1 )));} elseif (substr ($ v,) = "& #") {$ ar [$ k] = iconv ("UCS-2BE", "UTF-8", pack ("n ", substr ($ v, 2,-1) ;}} return join ("", $ ar) ;}// class end ?>
Demo. php
GetVideosInfo (); echo'';print_r($videosInfo);echo '
';?>
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] => [bicato ssss highlights !!!] Hong Kong bikachao exhibition [content] => there is a bikhao exhibition in Hong Kong. The exhibition will show the world's largest, 13-meter-tall "bikado stand boat 」, there will be 700 million users who are not playing the same role, of course, I am not too small to have a limited number of products, so I can try it out first, and I will try it out later, all of them have the same fun! The ONE x item is very refined. you want to confirm The date: November 9-20, 2014-January 4: 10am-10pm location: the ONE UG2 atrium [publish_date] => 1420715690 [length_seconds] => 124 [view_count] => 603 [avg_rating] => 0.0 [embed] => // www.youtube.com/embed/jYDwFozp6PY )... ..
Source code: Click to download
The above introduces php to get all Video information of a User on Youtube, including related content. I hope to help some friends who are interested in PHP tutorials.