2009-11-19 99 views
28

我現在玩Youtube API,我開始了一個小項目(爲了好玩)。如果我擁有視頻ID,我如何獲得YouTube視頻的標題?

問題是我無法找到從Id獲取視頻標題的方式。 (例如:ylLzyHk54Z0)

我看過DATA和PLAYER API文檔,找不到它。

如果有人知道如何做到這一點,或者如果有人可以幫助我找到辦法做到這一點,請幫助我。

注意:我正在使用javascript。這將是一個網絡應用程序。

編輯:我有一個想法。也許使用Regular表達式來解析頁面標題中的標題。我正在研究這個。

+3

不要使用RegEx或屏幕抓取!這個API可以幫你實現這一切...... – philfreo 2009-11-19 01:13:28

回答

35

不完全有可能在JavaScript,因爲你想從不同的域的文檔。如果你很樂意投入一點的PHP嘗試這一點。測試OK:

<? 
    $vidID = $_POST['vidID']; 
    $url = "http://gdata.youtube.com/feeds/api/videos/". $vidID; 
    $doc = new DOMDocument; 
    $doc->load($url); 
    $title = $doc->getElementsByTagName("title")->item(0)->nodeValue; 
?> 

<html> 
    <head> 
     <title>Get Video Name</title> 
    </head> 
    <body> 
     <form action="test.php" method="post"> 
      <input type="text" value="ID Here" name="vidID" /> 
      <input type="submit" value="Get Name" /> 
     </form> 
     <div id="page">URL: [<?= $url ?>]</div> 
     <div id="title">Title: [<?= $title ?>]</div> 
    </body> 
</html> 
+0

感謝您的回答。 是的,PHP不是問題。我準備了一個Ajax解決方案。 – 2009-11-19 02:00:58

+0

是的,它的一些可以在JavaScript中做不同的處理,但你仍然有這個跨域問題,沒有必要使它變得更復雜。如果你很高興,我希望你會接受答案:) – SimonDever 2009-11-19 02:06:33

+2

只需更新一下,現在就可以使用JavaScript [使用回調參數](http://code.google.com/apis/youtube/2.0/developers_guide_json)了。 HTML)。 – Marcel 2011-06-28 11:11:04

15

致電http://gdata.youtube.com/feeds/api/videos/ylLzyHk54Z0

在此XML文件中,請閱讀<title>標記的值。

YouTube Api Documentation

+11

不再有效。 – 2015-11-10 00:42:36

14

可以使用JSON請求:http://gdata.youtube.com/feeds/api/videos/ylLzyHk54Z0?v=2&alt=jsonc

+11

不再有效。 – 2015-11-10 00:41:39

1

我的解決辦法是:

$xmlInfoVideo = simplexml_load_file("http://gdata.youtube.com/feeds/api/videos/".$videoId."?v=2&fields=title"); 

foreach($xmlInfoVideo->children() as $title) { $videoTitle = strtoupper((string) $title); } 

這讓視頻的標題。

2

視頻標題是使用點符號的API和到達在JavaScript中:

the_name_of_your_video_object.A.videoData.title 
+1

我正在使用:'video_object.getVideoData()。title'。 – cdonts 2014-11-24 15:34:15

+0

@chester你的方式效果更好。 'A.videoData'不再適用於我。 – AaronS 2015-01-12 19:09:16

16

這是你如何可以用JavaScript和V3 YouTube數據API做到這一點。

var ytApiKey = "..."; 
var videoId = "ylLzyHk54Z0"; 

$.get("https://www.googleapis.com/youtube/v3/videos?part=snippet&id=" + videoId + "&key=" + ytApiKey, function(data) { 
    alert(data.items[0].snippet.title); 
}); 
+0

如何從視頻標題獲取視頻ID? – 2015-06-10 09:46:38

+0

這是API v3之後的新正確答案。 – 2016-08-03 19:46:20

4

這個答案是準確的,因爲2015年12月

要獲得來自YouTube視頻ID的視頻標題,你將不得不興建下面的網址,使用YouTube數據API(你需要使用API​​密鑰,否則請求會失敗):

https://www.googleapis.com/youtube/v3/videos?part=snippet&id={YOUTUBE_VIDEO_ID}&fields=items(id%2Csnippet)&key={YOUR_API_KEY}

做一個GET請求,你會得到類似下面的塊的JSON響應。對於標題,它存在於snippet/title密鑰中。

{ 
    "items":[ 
     { 
     "id":"Jglv0A0qLI8", 
     "snippet":{ 
      "publishedAt":"2014-06-30T03:42:20.000Z", 
      "channelId":"UCdTU5vd37FlTZ-xoB0xzRDA", 
      "title":"AIA Malaysia - A-Plus Venus Plan - Comprehensive Female Protection and Insurance Plan", 
      "description":"A comprehensive female protection plan for the modern women\n\nFor more information visit: http://www.aia.com.my/en/individuals/products-and-services/health/a-plus-venus-a-plus-venus-extra.html\n\nFor more products, visit AIA Malaysia's Products and Services playlist:\nhttps://www.youtube.com/playlist?list=PLSrgVT3aQ1fZ3SCe-dEVnFJDApBYkqolP\n\nFor more videos. subscribe to AIA Malaysia's YouTube channel:\nhttps://www.youtube.com/channel/UCdTU5vd37FlTZ-xoB0xzRDA", 
      "thumbnails":{ 
       "default":{ 
        "url":"https://i.ytimg.com/vi/Jglv0A0qLI8/default.jpg", 
        "width":120, 
        "height":90 
       }, 
       "medium":{ 
        "url":"https://i.ytimg.com/vi/Jglv0A0qLI8/mqdefault.jpg", 
        "width":320, 
        "height":180 
       }, 
       "high":{ 
        "url":"https://i.ytimg.com/vi/Jglv0A0qLI8/hqdefault.jpg", 
        "width":480, 
        "height":360 
       }, 
       "standard":{ 
        "url":"https://i.ytimg.com/vi/Jglv0A0qLI8/sddefault.jpg", 
        "width":640, 
        "height":480 
       }, 
       "maxres":{ 
        "url":"https://i.ytimg.com/vi/Jglv0A0qLI8/maxresdefault.jpg", 
        "width":1280, 
        "height":720 
       } 
      }, 
      "channelTitle":"AIA Malaysia", 
      "tags":[ 
       "aia", 
       "aia malaysia", 
       "a-plus venus", 
       "female health insurance", 
       "female life insurance", 
       "female insurance", 
       "female medical insurance" 
      ], 
      "categoryId":"27", 
      "liveBroadcastContent":"none", 
      "localized":{ 
       "title":"AIA Malaysia - A-Plus Venus Plan - Comprehensive Female Protection and Insurance Plan", 
       "description":"A comprehensive female protection plan for the modern women\n\nFor more information visit: http://www.aia.com.my/en/individuals/products-and-services/health/a-plus-venus-a-plus-venus-extra.html\n\nFor more products, visit AIA Malaysia's Products and Services playlist:\nhttps://www.youtube.com/playlist?list=PLSrgVT3aQ1fZ3SCe-dEVnFJDApBYkqolP\n\nFor more videos. subscribe to AIA Malaysia's YouTube channel:\nhttps://www.youtube.com/channel/UCdTU5vd37FlTZ-xoB0xzRDA" 
      } 
     } 
     } 
    ] 
} 

欲瞭解更多信息,請訪問the API documentation page

2

羅伯特·辛和cbaigorri的回答是最好的,這是在這個時候與JS做正確的方法,就GET請求:

https://www.googleapis.com/youtube/v3/videos?part=snippet&id={YOUTUBE_VIDEO_ID}&fields=items(id,snippet)&key={YOUR_API_KEY}

這個小規格: 你可以使用逗號分隔的YouTube視頻ID在一個請求中獲取多個視頻信息。

爲了得到1個視頻,取代{YOUTUBE_VIDEO_ID}與視頻ID(例如:123456) 要在一個請求得到更多的視頻,取代{YOUTUBE_VIDEO_ID}用逗號分隔的ID(例如:123456,234567,345678,456789

這將計爲單個請求在配額中,通過這種方式,您只需要1份配額/請求費用即可獲得大量視頻詳情。

相關問題