倡萌最近在为客户定制网站时,需要获取优酷视频缩略图,百度了一大圈,所得的方法无非都是:
- VideoUrlParser.class.php 这个类
- 沐风 Youku Videos 1.2.0 插件
- 其他代码版本……
查看了里面的代码,无非都是通过旧版本的优酷api获取数据:
很不幸的是,优酷已经升级这个API,无法再获取视频缩略图,你可以点击上面的链接地址,就可以得到如下如所示的内容:
{"data":[{"ct":"h","cs":"2197|2205|2223", "logo":"http:\/\/g1.ykimg.com\/", "seed":3970, "tags":["\u6211\u53ebMT\u63d2\u66f2","\u675c\u96ef\u5a9e","\u6211\u53ebMT"], "categories":"100", "videoid":"55121664", "vidEncoded":"XMjIwNDg2NjU2", "username":"\u9b3c\u0416\u6ce3", "userid":"3805457", "title":"\u675c\u96ef\u5a9e - \u96ea \u6211\u53ebMT\u63d2\u66f2", "up":0, "down":0, "ts":"8wWV1TDoSldjfN4B56Qwxg", "tsup":"8wWYHjPoSldjfN4CLKkwxg", "key1":"b148f43d", "key2":"789e55f11a15672c", "tt":"0", "videoSource":"1", "seconds":"226.35", "streamfileids":{"flv":"36*45*36*36*36*42*36*38*36*36*59*34*56*35*59*42*63*56*59*10*56*10*36*36*45*18*38*38*38*38*9*7*59*67*66*34*9*59*65*36*63*45*19*65*59*45*59*19*65*59*42*59*63*65*38*59*67*19*36*59*67*66*9*63*19*45*","mp4":"36*45*36*36*36*7*36*38*36*36*59*34*56*35*59*59*42*66*59*10*56*10*36*36*45*18*38*38*38*38*9*7*59*67*66*34*9*59*65*36*63*45*19*65*59*45*59*19*65*59*42*59*63*65*38*59*67*19*36*59*67*66*9*63*19*45*"}, "segs":{"flv":[{"no":0,"size":"7280601","seconds":226,"k":"1ec14cb1ea466078282abe64","k2":"1ee8c0b0e9c772956"}],"mp4":[{"no":0,"size":"14308548","seconds":226,"k":"4f57b6f585d4325b24125198","k2":"1def4abdbce3ab31b"}]}, "streamsizes":{"flv":"7280601","mp4":"14308548"},"stream_ids":{"flv":"121085627","mp4":"121085640"},"streamlogos":{"flv":1,"mp4":1},"streamtypes":["flv","mp4"],"streamtypes_o":["flv","flvhd","mp4"]}], "user":{"id":"792955424","acc_support":true}, "controller":{"search_count":true,"mp4_restrict":1,"stream_mode":1,"video_capture":true,"hd3_enabled":false,"area_code":450900,"dma_code":4134,"continuous":0,"playmode":"normal","circle":false,"tsflag":false,"other_disable":false,"xplayer_disable":false,"app_disable":false,"share_disabled":false,"download_disabled":false,"pc_disabled":false,"pad_disabled":false,"mobile_disabled":false,"tv_disabled":false,"comment_disabled":false}} |
从内容中再也找不到缩略图了。
没办法,倡萌只有去优酷官方查看新的API,新的 API 方式在:http://open.youku.com/docs?id=46,也就是通过 https://openapi.youku.com/v2/videos/show.json 接口,需要“视频id”和“应用key(client_id)”这两个参数。
所以我们要想办法获的“视频id”和“应用key(client_id)”,就可以使用这个接口获取数据。
所以,我们先注册优酷,创建应用,获得“应用key(client_id)”,具体步骤如下:
- 首先要拥有一个优酷帐号,并申请加入开发平台,点击这里注册优酷帐号
- 一个开发者帐号可以申请多个应用(client_id),点击这里申请
- 应用自动生效,可作为开发和测试使用。应用分为四个等级,普通、中级、高级和合作级。升级后不同等级应用可调用不同API。
- API访问频率限制:普通——每小时1000次,中级——每小时5万次,高级——每小时20万次,合作——无限制
有了 client_id,获取到的信息如下:
{"id":"XMjIwNDg2NjU2", "title":"\u675c\u96ef\u5a9e - \u96ea \u6211\u53ebMT\u63d2\u66f2", "link":"http:\/\/v.youku.com\/v_show\/id_XMjIwNDg2NjU2.html", "thumbnail":"http:\/\/r1.ykimg.com\/054204085264610F6A0A3F40A0CC261A", "bigThumbnail":"http:\/\/r1.ykimg.com\/054104085264610F6A0A3F40A0CC261A", "duration":"226.35", "category":"\u52a8\u6f2b", "state":"normal", "created":"2010-11-06 19:53:54", "published":"2010-11-06 19:58:21", "description":"\u675c\u96ef\u5a9e - \u96ea \u6211\u53ebMT\u63d2\u66f2", "player":"http:\/\/player.youku.com\/player.php\/sid\/XMjIwNDg2NjU2\/partnerid\/490ad1e99b548935\/v.swf", "public_type":"all", "copyright_type":"original", "user":{"id":"3805457","name":"\u9b3c\u0416\u6ce3","link":"http:\/\/v.youku.com\/user_show\/id_UMTUyMjE4Mjg=html"},"tags":"\u6211\u53ebMT\u63d2\u66f2,\u675c\u96ef\u5a9e,\u6211\u53ebMT","view_count":64838,"favorite_count":"230","comment_count":"55","up_count":"896","down_count":"17","operation_limit":[],"streamtypes":["flvhd","flv","hd","3gp","3gphd"],"source":{"id":"1","name":"\u4f18\u9177\u7ad9\u5185WEB\u4e0a\u4f20","link":"http:\/\/www.youku.com\/v_up\/"}} |
第4、5行 就是缩略图,前者小,后者大:
"thumbnail":"http:\/\/r1.ykimg.com\/054204085264610F6A0A3F40A0CC261A", "bigThumbnail":"http:\/\/r1.ykimg.com\/054104085264610F6A0A3F40A0CC261A", |
下面分享下具体的代码样例,懂代码的看下注释应该就明白了:
/** * WordPress 获取优酷缩略图 * 作者:倡萌@WordPress大学 * 出处:https://www.wpdaxue.com/wordPress-get-youku-video-thumb.html */ // 该函数用于获取优酷api地址的内容,在下一个函数中要使用 function getSslPage($url) { $ch = curl_init(); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ch, CURLOPT_HEADER, false); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_REFERER, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); $result = curl_exec($ch); curl_close($ch); return $result; } //该函数获取优酷视频缩略图 function get_youku_video_thumb(){ global $post; //检查该文章是否已经存储过优酷缩略图地址 if(get_post_meta($post->ID, '_youku_thumb', true )){ //已存储,就直接调用 $video_thumb = get_post_meta($post->ID, '_youku_thumb', true ); //如果没有存储,我们就需要通过优酷API获取 }else{ //开发者的client_id,每个ID都有调用次数限制,所以这里可以申请多个使用 //下面一行的2个 client_id 是无效的,请自己申请后,填入 $client_ids = array('d78e1f9c33332e3','490ad1e933338935'); //从所填入的 client_id 中随机选取一个 $num = array_rand($client_ids,1); $client_id = $client_ids[$num]; //在文章内容中比对获取优酷视频ID(在文章中单独一行添加视频播放页面的网址即可插入视频的) preg_match("#https?://v.youku.com/v_show/id_(?<video_id>[a-z0-9_=-]+)#i", $post->post_content, $matches); $cnt = count($matches); if ($cnt>0){ //获取到视频ID后,通过API读取缩略图 $link = "https://openapi.youku.com/v2/videos/show.json?video_id={$matches['video_id']}&client_id={$client_id}"; $cexecute= getSslPage($link); if ($cexecute) { //转换内容以供php读取 $result = json_decode($cexecute,true); $json = $result['data'][0]; //调用大缩略图 bigThumbnail $video_thumb = $result['bigThumbnail']; if($video_thumb !=''){ //将调用都的缩略图地址存储到文章字段中,以供后面调用,不需反复调用API update_post_meta( $post->ID, '_youku_thumb', $video_thumb ); } } }else{ //文章没有插入优酷视频的话,使用一个默认图片地址 $video_thumb = 'http://默认图片的网址,请自己填写'; } } return $video_thumb; } |
注:请务必在 30 行填入你申请到的 应用key(client_id)”,否则无法正常使用!!
在实际使用中,我们可以使用
<?php echo get_youku_video_thumb(); ?> |
输出视频缩略图的网址。
具体的演示地址,可以访问:http://www.daweijita.com/guitar_friend_works
PS:如果哪位朋友知道怎么将获取到的缩略图缓存到自己的主机的方法,请分享下给倡萌,谢谢
来源:
https://www.wpdaxue.com/wordpress-get-youku-video-thumb.html