2009-11-19 78 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份配额/请求费用即可获得大量视频详情。

相关问题