【问题标题】:Youtube feed array items in json showing as NULLjson 中的 Youtube 提要数组项显示为 NULL
【发布时间】:2013-06-25 11:41:25
【问题描述】:

我正在从视频中抓取 cmets,但“作者姓名”和他们的 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

所以我错过了什么?

【问题讨论】:

    标签: php json youtube-api


    【解决方案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 :)

    【讨论】:

    • 辛苦了。谢谢。顺便说一句....为什么 [0] 不在嵌套的其他项目上时需要介于两者之间?! (上面的例子除外)
    • 显然 youtube 允许,或者有某种可用性,其中每条评论都有多个作者。不知道为什么!
    猜你喜欢
    • 2013-08-04
    • 1970-01-01
    • 2021-12-15
    • 1970-01-01
    • 2017-02-14
    • 2018-09-11
    • 1970-01-01
    • 2011-04-25
    • 1970-01-01
    相关资源
    最近更新 更多