我一直在試圖訪問以下數組中的「視頻ID」 JSON對象:無法訪問JSON對象在PHP
{
"kind": "youtube#searchListResponse",
"etag": "\"m2yskBQFythfE4irbTIeOgYYfBU/652q8gXfNeBDSoSQrv8VCrAv0Ho\"",
"nextPageToken": "CAUQAA",
"regionCode": "AU",
"pageInfo": {
"totalResults": 25,
"resultsPerPage": 5
},
"items": [
{
"kind": "youtube#searchResult",
"etag": "\"m2yskBQFythfE4irbTIeOgYYfBU/31YuvB6enDzigibEfdgUq4-ZYc0\"",
"id": {
"kind": "youtube#video",
"videoId": "Ldx1nwBd4AY"
},
"snippet": {
"publishedAt": "2017-04-14T21:00:00.000Z",
"channelId": "UC-z95jtL6-oDyFueQPoZLfQ",
"title": "HOW TO PLAY NINTENDO DS GAMES ON iPHONE iOS 10 FOR FREE (NDS4iOS) - NDS4IOS Tutorial - Tech Scout",
"description": "LIKE THE VIDEO + TURN ON CHANNEL NOTIFICATIONS! HOW TO PLAY NINTENDO DS GAMES ON iPHONE iOS 10 FOR FREE (NDS4iOS) - NDS4IOS ...",
"thumbnails": {
"default": {
"url": "https://i.ytimg.com/vi/Ldx1nwBd4AY/default.jpg",
"width": 120,
"height": 90
},
"medium": {
"url": "https://i.ytimg.com/vi/Ldx1nwBd4AY/mqdefault.jpg",
"width": 320,
"height": 180
},
"high": {
"url": "https://i.ytimg.com/vi/Ldx1nwBd4AY/hqdefault.jpg",
"width": 480,
"height": 360
}
},
"channelTitle": "Tech Scout",
"liveBroadcastContent": "none"
}
},
它從那裏繼續,但我一直在試圖訪問「視頻ID」使用下面的代碼對象:
$youtubeurl = "https://www.googleapis.com/youtube/v3/search?order=date&part=snippet&channelId=" . $channelid . "&key=" . $api;
$youtubeinfo = file_get_contents($youtubeurl);
$youtubeencoded = json_decode($youtubeinfo, true);
$youtubevideoid = $youtubeencoded["data"]["items"][0]["videoID"];
echo $youtubevideoid;
我沒有與JSON太多的經驗,所以我剛剛在第4行的代碼(我認爲這是問題)從堆棧溢出的另一個問題。
你可以做' print_r($ youtubeencoded)'查看解析的php數組結構 –