2012-09-10 196 views
0

好吧,爲WordPress開發一個基於vimeo的視頻博客主題,而不是懶惰地要求人們將嵌入代碼粘貼到每個視頻更新中(通過自定義字段),我會而是用戶提供的只是視頻ID ...基本上我不能保證每個視頻將會是相同的高度/寬度,所以我試圖直接從vimeo獲取嵌入代碼(而不是讓用戶提供),而不是硬編碼的iframe的高度/寬度(因爲主題將作出響應,我使用小jquery腳本來動態調整視頻大小)獲取特定vimeo視頻的嵌入代碼,vimeo API

到目前爲止,已經試圖屠殺Vimeo自己的API示例,我有這個:

function curl_get($url) { 
    $curl = curl_init($url); 
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt($curl, CURLOPT_TIMEOUT, 30); 
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); 
    $return = curl_exec($curl); 
    curl_close($curl); 
    return $return; 
} 

// Create the URL 
$oembed_vid_id = get_field('video_embed_code'); 
$oembed_url = 'http://vimeo.com/api/v2/video/' . $oembed_vid_id . '.xml'; 

// Load in the oEmbed XML 
$oembed = simplexml_load_string(curl_get($oembed_url)); 
$embed_code = html_entity_decode($oembed->html); 

<?php echo $embed_code ?> 

但是我沒有收回任何東西,我忽略了一些非常基本的東西?

我可以證實,$ oembed_url URL確實存在,請嘗試VIMEO ID,你會看到它的相關的XML,即這裏是我從VIMEO彈撥視頻:

http://vimeo.com/api/v2/video/48198301.xml

如果我能夠掌握這一點,那將是太棒了,因爲我可以拉下縮略圖等,並利用xml文件中隱藏的所有其他可愛信息。

回答

1

Vimeo's Sample Code,似乎你沒有使用正確的API。

http://vimeo.com/api/v2/video/將返回視頻信息字段,但沒有html字段。要獲得嵌入代碼,使用這個網址:

$oembed_url = 'http://vimeo.com/api/oembed.xml?url=' . urlencode("http://vimeo.com/$oembed_vid_id"); 
+0

出於某種原因,那原來的API是我有麻煩,有一些谷歌上搜索我與v2的鏈接取代它的人 - 但一切似乎現在的工作,謝謝!可能是urlencode的一部分,因爲我最初的版本是$ oembed_url ='http://vimeo.com/etc..?url=http://vimeo.com/'。 $ oembed_vid_id; 儘管完美,謝謝:) – efreeman