0

我已经修改了来自youtube 的代码,获取视频的元数据,以便能够打印元数据。它工作正常,除了最后一行没有打印标题。

$url = ("PUT A YOUTUBE URL HERE"]);
$youtube = "http://www.youtube.com/oembed?url=".$url."&format=json";
$json =  file_get_contents($youtube);

printf('<br />vid_code is %s', $row["vid_code"]);
printf('<br />vid_url is %s', $row["vid_url"]);
printf('<br />$url is %s', $url);
printf('<br />$youtube is %s', $youtube);
printf('<br />$json is %s', $json);
printf ('<br />Title is: '.$json->title);   //THIS IS THE LINE THAT IS NOT WORKING

看起来很简单......我错过了什么?

4

2 回答 2

1

我无法完全识别这种方法-> https://developers.google.com/youtube/2.0/developers_guide_json

我会做类似的事情:

$url = "OINa46HeWg8"; //some random video
$youtube = 'http://gdata.youtube.com/feeds/api/videos/'.$url.'?v=2&alt=jsonc';
$json = file_get_contents($youtube);

$json = json_decode($json, true);
$title = $json['data']['title'];
echo $title;

输出:

I Forgot My Phone

HTML:

<h1><? echo $title;?></h1>
<iframe width="420" height="315" src="//www.youtube.com/embed/<? echo $url;?>" frameborder="0" allowfullscreen></iframe>

容易得多,而且没有开销。

于 2013-09-02T21:40:24.937 回答
0

你忘了解码 json,使用json_decode()

这是一个例子:

<?php
$url = "http://www.youtube.com/watch?v=-iMR0uac2Kg";
$youtube = "http://www.youtube.com/oembed?url=".urlencode($url)."&format=json";
$json =  json_decode(file_get_contents($youtube));

echo '<pre>'.print_r($json,true).'</pre>';
/*
stdClass Object
(
    [author_name] => Haim Michael
    [version] => 1.0
    [width] => 480
    [type] => video
    [provider_url] => http://www.youtube.com/
    [height] => 270
    [author_url] => http://www.youtube.com/user/lifemichael
    [provider_name] => YouTube
    [thumbnail_width] => 480
    [thumbnail_height] => 360
    [html] => <iframe width="480" height="270" src="http://www.youtube.com/embed/-iMR0uac2Kg?feature=oembed" frameborder="0" allowfullscreen></iframe>
    [title] => The json_decode Function in PHP
    [thumbnail_url] => http://i1.ytimg.com/vi/-iMR0uac2Kg/hqdefault.jpg
)
*/

//So to get the title:
echo $json->title;
?>
于 2013-09-02T21:23:12.823 回答