doude2635 2017-08-15 01:59
浏览 72
已采纳

Foreach循环PHP错误

I want to loop the "textDisplay" using foreach but im not sure which part of my php code is wrong and it gives me a "Trying to get property of non-object " error

XML

<pre>
{
"kind": "youtube#commentThreadListResponse",
"etag": "\"m2yskBQFythfE4irbTIeOgYYfBU/o6YjewN3UppKqc9x-ZYYa5xYhA8\"",
"pageInfo": {
    "totalResults": 9,
    "resultsPerPage": 20
},
"items": [
    {
        "kind": "youtube#commentThread",
        "etag": "\"m2yskBQFythfE4irbTIeOgYYfBU/uE9QsmedbKmEauRAmmwW18vNQa8\"",
        "id": "z12qxfxr2onpy1b5l04cdfzrgwabir0q4bo",
        "snippet": {
            "videoId": "Au87oAJ2jeE",
            "topLevelComment": {
                "kind": "youtube#comment",
                "etag": "\"m2yskBQFythfE4irbTIeOgYYfBU/EUV0UwLw788gwYsvyDO2xMRjG8w\"",
                "id": "z12qxfxr2onpy1b5l04cdfzrgwabir0q4bo",
                "snippet": {
                    "authorDisplayName": "Randy Taschner",
                    "authorProfileImageUrl": "https://yt3.ggpht.com/--vE0X3_vDCs/AAAAAAAAAAI/AAAAAAAAAAA/P6kgycrPEZw/s28-c-k-no-mo-rj-c0xffffff/photo.jpg",
                    "authorChannelUrl": "http://www.youtube.com/channel/UCTRuBHRb4BRFcob-hMj6NnQ",
                    "authorChannelId": {"value": "UCTRuBHRb4BRFcob-hMj6NnQ"},
                    "videoId": "Au87oAJ2jeE",
                    "textDisplay": "Thank you Dan and Envato for creating this video!",
                    "textOriginal": "Thank you Dan and Envato for creating this video!",
                    "canRate": true,
                    "viewerRating": "none",
                    "likeCount": 1,
                    "publishedAt": "2015-08-16T05:02:25.000Z",
                    "updatedAt": "2015-08-16T05:02:25.000Z"
                }
            },
                "canReply": true,
                "totalReplyCount": 1,
                "isPublic": true
        }
    }
]

}

MY PHP CODE

$json = file_get_contents('https://www.googleapis.com/youtube/v3/commentThreads?part=snippet%2Creplies&videoId='.$videoid.'&key='.$apikey);
$ytdata = json_decode($json);
foreach($ytdata->items[0]->snippet->topLevelComment->snippet->textDisplay as $hit){
    echo $hit;
}

Thanks

  • 写回答

1条回答 默认 最新

  • dongyouji7022 2017-08-15 02:05
    关注

    $ytdata->items[0]->snippet->topLevelComment->snippet->textDisplay is not an array - it's a string.

    Perhaps you meant to loop the items?

    foreach ($ytdata->items as $item) {
        echo $item->snippet->topLevelComment->snippet->textDisplay;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启