2017-08-15 180 views
0

我想環路「textDisplay形式」使用的foreach但林不知道這是我的PHP代碼的部分是錯誤的,它給了我一個「試圖獲得非對象的屬性」錯誤的foreach循環PHP錯誤

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 
     } 
    } 
] 

}

我的PHP代碼

 
$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; 
}

感謝

回答

1

$ytdata->items[0]->snippet->topLevelComment->snippet->textDisplay不是數組 - 它是一個字符串。

也許你打算循環的項目?

foreach ($ytdata->items as $item) { 
    echo $item->snippet->topLevelComment->snippet->textDisplay; 
} 
+0

謝謝@Scopey :) –