WordPress 获取优酷视频缩略图(新版)

我从窗户里探头往外看,嘿!春天果然到来了。看,外面嫩绿的小草像动画片里的那样,慢慢探出头来。再看,那平坦的草地里,星星点点的眨着眼睛的是什么?哦!那是可爱的小花,还有小虫在花瓣里钻来钻去呢?嘻,原来是童话故事里睡在花瓣里的拇指姑娘啊!再看看,那干枯已久的柳树也伸出了嫩绿的手,轻轻地走来了美丽的春姑娘!

倡萌最近在为客户定制网站时,需要获取优酷视频缩略图,百度了一大圈,所得的方法无非都是:

  1. VideoUrlParser.class.php 这个类
  2. 沐风 Youku Videos 1.2.0 插件
  3. 其他代码版本……

查看了里面的代码,无非都是通过旧版本的优酷api获取数据:

http://v.youku.com/player/getPlayList/VideoIDS/XMjIwNDg2NjU2/timezone/+08/version/5/source/out?password=&ran=2513&n=3

很不幸的是,优酷已经升级这个API,无法再获取视频缩略图,你可以点击上面的链接地址,就可以得到如下如所示的内容:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
{"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}}

{"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)”,具体步骤如下:

    1. 首先要拥有一个优酷帐号,并申请加入开发平台,点击这里注册优酷帐号
    2. 一个开发者帐号可以申请多个应用(client_id),点击这里申请
    3. 应用自动生效,可作为开发和测试使用。应用分为四个等级,普通、中级、高级和合作级。升级后不同等级应用可调用不同API。
    4. API访问频率限制:普通——每小时1000次,中级——每小时5万次,高级——每小时20万次,合作——无限制

有了 client_id,获取到的信息如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
{"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\/"}}

{"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行 就是缩略图,前者小,后者大:

1
2
"thumbnail":"http:\/\/r1.ykimg.com\/054204085264610F6A0A3F40A0CC261A",
"bigThumbnail":"http:\/\/r1.ykimg.com\/054104085264610F6A0A3F40A0CC261A",

"thumbnail":"http:\/\/r1.ykimg.com\/054204085264610F6A0A3F40A0CC261A", "bigThumbnail":"http:\/\/r1.ykimg.com\/054104085264610F6A0A3F40A0CC261A",

下面分享下具体的代码样例,懂代码的看下注释应该就明白了:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/**
 * 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;
}

/** * 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)”,否则无法正常使用!!

在实际使用中,我们可以使用

1
<?php echo get_youku_video_thumb(); ?>

<?php echo get_youku_video_thumb(); ?>

输出视频缩略图的网址。

具体的演示地址,可以访问:http://www.daweijita.com/guitar_friend_works

PS:如果哪位朋友知道怎么将获取到的缩略图缓存到自己的主机的方法,请分享下给倡萌,谢谢

本文WordPress 获取优酷视频缩略图(新版)到此结束。爱自己胜于爱他人,才是自然和真实的。小编再次感谢大家对我们的支持!

您可能有感兴趣的文章
WordPress站点Gravatar头像前后台不显示的如何解决办法

WordPress主题需要支持https吗?WordPress站点如何如何实现https?

WordPress站点的页面/标签/分类URL地址如何添加.html?

WordPress站点更换了域名后数据库应该如何操作替换新旧域名?

WordPress安装在主机空间的什么目录里面?根目录在哪里?