WordPress 獲取優酷視頻縮略圖(新版)

最近在為客戶定制網站時,需要獲取優酷視頻縮略圖,百度瞭一大圈,所得的方法無非都是:

VideoUrlParser.class.php 這個類沐風 Youku Videos 1.2.0 外掛其他代碼版本……

查看瞭裡面的代碼,無非都是通過舊版本的優酷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)”,具體步驟如下:

首先要擁有一個優酷帳號,並申請加入開發平臺,點擊這裡註冊優酷帳號一個開發者帳號可以申請多個應用(client_id),點擊這裡申請應用自動生效,可作為開發和測試使用。應用分為四個等級,普通、中級、高級和合作級。升級後不同等級應用可調用不同API。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

2015-08-27_125644_wpdaxue_com

PS:如果哪位朋友知道怎麼將獲取到的縮略圖緩存到自己的主機的方法,請分享下給,謝謝

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *