PHP语言 获取Youtube某个User所有Video信息
小标 2018-07-12 来源 : 阅读 1179 评论 0

摘要:本文主要向大家介绍了PHP语言 获取Youtube某个User所有Video信息,通过具体的实例向大家展示,希望对大家学习php语言有所帮助。

本文主要向大家介绍了PHP语言 获取Youtube某个User所有Video信息,通过具体的实例向大家展示,希望对大家学习php语言有所帮助。

因工作需要,获取Youtube上某个用户的所有视频信息,写了这个类,分享给大家。

 

YTUserVideo.class.php

[php] view plain copy

1. <?php  

2. /** 获取Youtube某个User所有Video信息 

3. *   Date:   2015-01-08 

4. *   Author: fdipzone 

5. *   Ver:    1.0 

6. * 

7. *   Func: 

8. *   public  getVideosInfo 获取用户所有视频信息 

9. *   private getVideoNum   获取用户视频数量 

10. *   private getVideoInfo  获取视频信息 

11. *   private getContent    视频简介整理 

12. *   private unescape      unicode转中文 

13. */  

14.   

15. class YTUserVideo{ // class start  

16.   

17.     private $_user = ''; // 用户名称  

18.   

19.   

20.     /** 初始化 

21.     * @param String $user 用户名称 

22.     */  

23.     public function __construct($user=''){  

24.         if($user!=''){  

25.             $this->_user = $user;  

26.         }else{  

27.             throw new Exception("user is empty", 1);  

28.         }  

29.     }  

30.   

31.   

32.     /** 获取user所有视频信息 

33.     * @return Array 

34.     */  

35.     public function getVideosInfo(){  

36.   

37.         $info = array();  

38.   

39.         // 获取视频数量  

40.         $videonum = $this->getVideoNum();  

41.   

42.         // 获取视频信息  

43.         for($i=1; $i<=$videonum; $i++){  

44.             $videoInfo = $this->getVideoInfo($i);  

45.             array_push($info, $videoInfo);  

46.         }  

47.   

48.         return $info;  

49.   

50.     }  

51.   

52.   

53.     /** 获取用户视频数量 

54.     * @return int 

55.     */  

56.     private function getVideoNum(){  

57.         $videos = simplexml_load_file('//gdata.youtube.com/feeds/base/users/'.$this->_user.'/uploads?max-results=1&start-index=1');  

58.         $videonum = $videos->children('openSearch', true)->totalResults;  

59.         return $videonum;  

60.     }  

61.   

62.   

63.     /** 获取视频信息 

64.     * @param  String $index 视频的序号 

65.     * @return Array 

66.     */  

67.     private function getVideoInfo($index){  

68.   

69.         // 获取视频id及简介  

70.         $video = simplexml_load_file('//gdata.youtube.com/feeds/base/users/'.$this->_user.'/uploads?max-results=1&start-index='.$index);  

71.         $videoId = str_replace('//gdata.youtube.com/feeds/base/videos/', '', (string)($video->entry->id));  

72.         $videoContent = $this->getContent($video->entry->content);  

73.         $videoPublish = strtotime($video->entry->published);  

74.   

75.         // 根据视频id获取视频信息  

76.         $content = file_get_contents('//youtube.com/get_video_info?video_id='.$videoId);  

77.         parse_str($content, $ytarr);  

78.   

79.         $info = array();  

80.   

81.         $info['id'] = $videoId;  

82.         $info['thumb_photo'] = $ytarr['thumbnail_url'];       // 缩略图  

83.         $info['middle_photo'] = $ytarr['iurlmq'];             // 中图  

84.         $info['big_photo'] = $ytarr['iurl'];                  // 大图  

85.         $info['title'] = $ytarr['title'];                     // 标题  

86.         $info['content'] = $videoContent;                     // 简介  

87.         $info['publish_date'] = $videoPublish;                // 发布时间  

88.         $info['length_seconds'] = $ytarr['length_seconds'];   // 视频长度(s)  

89.         $info['view_count'] = $ytarr['view_count'];           // 观看次数  

90.         $info['avg_rating'] = $ytarr['avg_rating'];           // 平均评分  

91.         $info['embed'] = '//www.youtube.com/embed/'.$videoId; // Embed  

92.   

93.         return $info;  

94.   

95.     }  

96.   

97.   

98.     /** 获取视频简介 

99.     * @param  String $content 内容 

100.     * @return String 

101.     */  

102.     private function getContent($content){  

103.         preg_match('/<span>(.*?)<\/span>/is', $content, $matches);  

104.         return $this->unescape($matches[1]);  

105.     }  

106.   

107.   

108.     /* unicode 转 中文 

109.     * @param  String $str unicode 字符串 

110.     * @return String 

111.     */  

112.     private function unescape($str) {  

113.         $str = rawurldecode($str);  

114.         preg_match_all("/(?:%u.{4})|&#x.{4};|&#\d+;|.+/U",$str,$r);  

115.         $ar = $r[0];  

116.   

117.         foreach($ar as $k=>$v) {  

118.             if(substr($v,0,2) == "%u"){  

119.                 $ar[$k] = iconv("UCS-2BE","UTF-8",pack("H4",substr($v,-4)));  

120.             }elseif(substr($v,0,3) == "&#x"){  

121.                 $ar[$k] = iconv("UCS-2BE","UTF-8",pack("H4",substr($v,3,-1)));  

122.             }elseif(substr($v,0,2) == "&#") {  

123.                 $ar[$k] = iconv("UCS-2BE","UTF-8",pack("n",substr($v,2,-1)));  

124.             }  

125.         }  

126.         return join("",$ar);  

127.     }  

128.   

129. } // class end  

130.   

131. ?>  

demo.php

[php] view plain copy

1. <?php  

2. require 'YTUserVideo.class.php';  

3.   

4. $obj = new YTUserVideo('GOtriphk'); // 用户名称GOtriphk https://www.youtube.com/user/GOtriphk/videos  

5. $videosInfo = $obj->getVideosInfo();  

6.   

7. echo '<pre>';  

8. print_r($videosInfo);  

9. echo '</pre>';  

10. ?>  


输出:

[plain] view plain copy

1. Array  

2. (  

3.     [0] => Array  

4.         (  

5.             [id] => jYDwFozp6PY  

6.             [thumb_photo] => //i.ytimg.com/vi/jYDwFozp6PY/default.jpg  

7.             [middle_photo] => //i.ytimg.com/vi/jYDwFozp6PY/mqdefault.jpg  

8.             [big_photo] => //i.ytimg.com/vi/jYDwFozp6PY/hqdefault.jpg  

9.             [title] => 【比卡超ssss突襲尖咀!!!】香港比卡超展  

10.             [content] => 香港有比卡超展,同場會展出全球最大、高13米嘅「比卡超立體飛船」,仲會有700隻唔同角色嘅精靈現身,當然亦唔小得又勁多期間限定紀念品可以優先搶以及由橫濱專程到港嘅聖誕版比卡超同粉絲全接觸,總之飛唔飛都一樣有得玩!The ONE x 寵物小精靈 聖誕夢想飛行日期:2014年11月9日至2015年1月4日時間:10am-10pm地點:The ONE UG2 中庭  

11.             [publish_date] => 1415257662  

12.             [length_seconds] => 124  

13.             [view_count] => 603  

14.             [avg_rating] => 0.0  

15.             [embed] => //www.youtube.com/embed/jYDwFozp6PY  

16.         )  

17. .....  

 

本文由职坐标整理并发布,了解更多内容,请关注职坐标编程语言PHP频道!


本文由 @小标 发布于职坐标。未经许可,禁止转载。
喜欢 | 1 不喜欢 | 0
看完这篇文章有何感觉?已经有1人表态,100%的人喜欢 快给朋友分享吧~
评论(0)
后参与评论

您输入的评论内容中包含违禁敏感词

我知道了

助您圆梦职场 匹配合适岗位
验证码手机号,获得海同独家IT培训资料
选择就业方向:
人工智能物联网
大数据开发/分析
人工智能Python
Java全栈开发
WEB前端+H5

请输入正确的手机号码

请输入正确的验证码

获取验证码

您今天的短信下发次数太多了,明天再试试吧!

提交

我们会在第一时间安排职业规划师联系您!

您也可以联系我们的职业规划师咨询:

小职老师的微信号:z_zhizuobiao
小职老师的微信号:z_zhizuobiao

版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
 沪公网安备 31011502005948号    

©2015 www.zhizuobiao.com All Rights Reserved

208小时内训课程