0

我正在从视频中获取评论,但“作者姓名”和他们的 ID 为 NULL。

这就是我所拥有的。

$feedUrl='http://gdata.youtube.com/feeds/api/videos/'.$selected_video_id.'/comments?v=2&alt=json';  
$data = json_decode(file_get_contents($feedUrl),true);
$info = $data["feed"];
$entry = $info["entry"];
$nEntry = count($entry);

for($i=0;$i<$nEntry;$i++){ 
    $name =  $entry[$i]['author']['name']['$t'];
    $userId = $entry[$i]['author']['yt$userId']['$t'];
    $content = $entry[$i]['content']['$t'];
    $published_2 = $entry[$i]['published']['$t'];
}

Content 和 Published 收集得很好,但 name 和 userID 没有。

提要中确实包含在 youtube 数据 api 演示测试版中查看的元素。如果您在浏览器中执行提要请求,它还会显示所有内容。

http://gdata.youtube.com/feeds/api/videos/VIDEO_ID/comments

所以我错过了什么吗?

4

1 回答 1

1

它应该是:

$name =  $entry[$i]['author'][0]['name']['$t'];
$userId = $entry[$i]['author'][0]['yt$userId']['$t'];
$content = $entry[$i]['content']['$t'];
$published_2 = $entry[$i]['published']['$t'];

或者更好:

$feedUrl=file_get_contents('http://gdata.youtube.com/feeds/api/videos/ASO_zypdnsQ/comments?v=2&alt=json'); 
$json = json_decode($feedUrl, true);
foreach($json['feed']['entry'] as $entry) {
    echo $entry['author'][0]['name']['$t']."<br>";
    echo $entry['author'][0]['yt$userId']['$t']."<br>";
    echo $entry['content']['$t']."<br>";
    echo $entry['published']['$t']."<br>";
}

你可以foreach像上面那样使用:)

于 2013-06-27T20:13:42.867 回答