Php obtains all Video information of a User on Youtube,

Source: Internet
Author: User

Php obtains all Video information of a User on Youtube,

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

<? Php/** get all Video information of a User on Youtube * Date: * Author: fdipzone * Ver: 1.0 ** Func: * public getVideosInfo: Get all user video information * private getVideoNum get user video quantity * private getVideoInfo get video information * private getContent video overview sort out * private unescape unicode to Chinese */class YTUserVideo {// class start private $ _ user = ''; // user name/** initialization * @ param String $ user name */public function _ construct ($ user = '') {if ($ user! = '') {$ This-> _ 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/'.w.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/'.w.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 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 '] = $ yta Rr ['length _ 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> (. *?) <\/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

<? Phprequire ''tuservideo. class. php '; $ obj = new YTUserVideo ('gotriphk'); // user name GOtriphk response = $ 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] => [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


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.