У меня проблема с созданием массива для foreach(){}
для json, который использует curl для получения json из youtube. У меня уже есть декодированный json для части curl, но когда я попытался декодировать, используя true, чтобы получить массив для json_decode
, я не могу получить массив для каждого из выходных данных массива. Как мне получить каждый элемент и выставить его в виде массива?
Я уже пробовал этот сайт:
Расшифровка JSON с использованием PHP
, чтобы получить некоторую помощь, но она не работает.
////////youtube api////////
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://www.googleapis.com/youtube/v3/search?part=snippet&maxResults=5&order=rating&q=resident%20evil&type=video&videoEmbeddable=true&key=",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err)
{
echo "cURL Error #:" . $err;
} else
{
echo $response;
//echo json_decode($response, true);
$json_array = (array) json_decode($response, true);
foreach($json_array as $json)
{
//echo $json['title']; // you can access your key value like this if result is array
//echo $json['kind'][0];
// echo $json['id'];
//echo $json->title; // you can access your key value like this if result is object
//
// $error = json_last_error();
// var_dump($json, $error === JSON_ERROR_UTF8);
//print $json->response->kind[0];
//echo $error;
}
}
/////youtube api//////
OUTPUT JSON:
{
"kind": "youtube#searchListResponse",
"etag": "\"p4VTdlkQv3HQeTEaXgvLePAydmU/l9fetqKKtU8Szcemtrp1SkvVqXU\"",
"nextPageToken": "CAUQAA",
"regionCode": "US",
"pageInfo": {
"totalResults": 1000000,
"resultsPerPage": 5
},
"items": [{
"kind": "youtube#searchResult",
"etag": "\"p4VTdlkQv3HQeTEaXgvLePAydmU/ycGvBReC7A0jFAVEINKomlikUpk\"",
"id": {
"kind": "youtube#video",
"videoId": "_SzgAVZUl78"
},
"snippet": {
"publishedAt": "2019-10-04T15:51:00.000Z",
"channelId": "UCeXaWu8hgGxjnOwM2r4Zu_A",
"title": "Resident Evil llega a Monster Hunter World: Iceborne",
"description": "Cazadores algo sobrenatural esta sucediento en Monster Hunter: World, y es por ello que Leon y Claire de Resident Evil, llegarán al juego este noviembre ...",
"thumbnails": {
"default": {
"url": "https://i.ytimg.com/vi/_SzgAVZUl78/default.jpg",
"width": 120,
"height": 90
},
"medium": {
"url": "https://i.ytimg.com/vi/_SzgAVZUl78/mqdefault.jpg",
"width": 320,
"height": 180
},
"high": {
"url": "https://i.ytimg.com/vi/_SzgAVZUl78/hqdefault.jpg",
"width": 480,
"height": 360
}
},
"channelTitle": "Arata Latam",
"liveBroadcastContent": "none"
}
}, {
"kind": "youtube#searchResult",
"etag": "\"p4VTdlkQv3HQeTEaXgvLePAydmU/Cz2nZtIJefFeGMYIUIS52KQzRsI\"",
"id": {
"kind": "youtube#video",
"videoId": "gE2EGGstC_4"
},
"snippet": {
"publishedAt": "2018-02-04T18:29:52.000Z",
"channelId": "UCaddCVMWjvDI_rfWZML8qvw",
"title": "RESIDENT EVIL - REVELATIONS [#12] THE QUEEN IS DEAD",
"description": "WALKTHROUGH OF ALL EPISODES OF THE QUEEN IS DEAD. NO COMMENTARY. NORMAL DIFFICULTY. HD.",
"thumbnails": {
"default": {
"url": "https://i.ytimg.com/vi/gE2EGGstC_4/default.jpg",
"width": 120,
"height": 90
},
"medium": {
"url": "https://i.ytimg.com/vi/gE2EGGstC_4/mqdefault.jpg",
"width": 320,
"height": 180
},
"high": {
"url": "https://i.ytimg.com/vi/gE2EGGstC_4/hqdefault.jpg",
"width": 480,
"height": 360
}
},
"channelTitle": "Wormwoodmachine",
"liveBroadcastContent": "none"
}
}, {
"kind": "youtube#searchResult",
"etag": "\"p4VTdlkQv3HQeTEaXgvLePAydmU/pYPZMJ3MHu9g7tghE7einGWzLSo\"",
"id": {
"kind": "youtube#video",
"videoId": "4w7ISumKoe4"
},
"snippet": {
"publishedAt": "2010-08-01T20:01:50.000Z",
"channelId": "UCbaXoO4Lnw826akSES9J51w",
"title": "Biohazard / Resident Evil - Sega Saturn",
"description": "http://www.thearchive.tk.",
"thumbnails": {
"default": {
"url": "https://i.ytimg.com/vi/4w7ISumKoe4/default.jpg",
"width": 120,
"height": 90
},
"medium": {
"url": "https://i.ytimg.com/vi/4w7ISumKoe4/mqdefault.jpg",
"width": 320,
"height": 180
},
"high": {
"url": "https://i.ytimg.com/vi/4w7ISumKoe4/hqdefault.jpg",
"width": 480,
"height": 360
}
},
"channelTitle": "Nakian",
"liveBroadcastContent": "none"
}
}, {
"kind": "youtube#searchResult",
"etag": "\"p4VTdlkQv3HQeTEaXgvLePAydmU/uEOwZoLXlcKYQTs2Vvp-06qtS4Q\"",
"id": {
"kind": "youtube#video",
"videoId": "1g2ems2cW2I"
},
"snippet": {
"publishedAt": "2019-01-28T15:51:45.000Z",
"channelId": "UCAbZF8x8TbsooEWfJ0fA-Cw",
"title": "Resident Evil 2 Remake Last Judgment",
"description": "Resident Evil 2 Remake (Soundtrack)",
"thumbnails": {
"default": {
"url": "https://i.ytimg.com/vi/1g2ems2cW2I/default.jpg",
"width": 120,
"height": 90
},
"medium": {
"url": "https://i.ytimg.com/vi/1g2ems2cW2I/mqdefault.jpg",
"width": 320,
"height": 180
},
"high": {
"url": "https://i.ytimg.com/vi/1g2ems2cW2I/hqdefault.jpg",
"width": 480,
"height": 360
}
},
"channelTitle": "Black Pain",
"liveBroadcastContent": "none"
}
}, {
"kind": "youtube#searchResult",
"etag": "\"p4VTdlkQv3HQeTEaXgvLePAydmU/hyeTWi-c-0xib8C7ZRuxjZGT7bE\"",
"id": {
"kind": "youtube#video",
"videoId": "tud8tUdC7OM"
},
"snippet": {
"publishedAt": "2019-10-04T20:06:42.000Z",
"channelId": "UChWTmWEZBWLroVBsB9MvU2A",
"title": "Resident Evil 2 R2MAKE Hardcore Leon B",
"description": "Thy kingdom come, Thy will be done. We play all kinds of games. Silent hill, batman, Resident evil, metal gear, h1z1, fortnite, roblox, dead space, evil within, god ...",
"thumbnails": {
"default": {
"url": "https://i.ytimg.com/vi/tud8tUdC7OM/default.jpg",
"width": 120,
"height": 90
},
"medium": {
"url": "https://i.ytimg.com/vi/tud8tUdC7OM/mqdefault.jpg",
"width": 320,
"height": 180
},
"high": {
"url": "https://i.ytimg.com/vi/tud8tUdC7OM/hqdefault.jpg",
"width": 480,
"height": 360
}
},
"channelTitle": "Tu Papi Leon",
"liveBroadcastContent": "none"
}
}]
}