我已经制作了一个简单的代码来在PHP和JSON中拥有视频youtube的标题,但是我现在遇到了问题。
这是我的代码:
<?php
$url = 'http://gdata.youtube.com/feeds/api/videos/';
$vid = $video['video_id'];
$end = '?format=5&alt=json';
$response = file_get_contents($url.$vid.$end);
$obj = json_decode($response);
?>
<?php print_r($obj->entry->title); ?>
我的print_r
打印这个:
stdClass 对象 ( [$t] => 视频标题 [类型] => 文本 )
我应该如何获得这个$t
?
使用第二个参数设置为 true
的 json_decode
,这将创建一个关联数组而不是stdClass
对象。然后像这样打印:
json_decode($response, true);
print_r($obj['entry']['title']['$t'];
使用这个
$obj = json_decode($response, true);
foreach($obj['data']['items'] as $item) {
echo $item['title'];
echo $item['description'];
echo $item['rating'];
echo $item['player']['default'];
// whatever you need....
}