Let's say I do a simple youtube search using the YouTube API v3, and I get a response like this:
{
"kind": "youtube#searchListResponse",
"etag": "\"m2yskBQFythfE4irbTIeOgYYfBU/1MwZbx-vX21eNjUJjkUlNHKCIhI\"",
"nextPageToken": "CAIQAA",
"regionCode": "US",
"pageInfo": {
"totalResults": 412,
"resultsPerPage": 2
},
"items": [
{
"kind": "youtube#searchResult",
"etag": "\"m2yskBQFythfE4irbTIeOgYYfBU/q2KHbIuwnAhM64HgrqhBqc5xvTI\"",
"id": {
"kind": "youtube#video",
"videoId": "gpZvuZEiINA"
},
"snippet": {
"publishedAt": "2014-03-30T11:46:50.000Z",
"channelId": "UCCaE0Bj6NI-y8_yL1FpcJUw",
"title": "Depression-90` instrumental (download link)",
"description": "E-Mail: ensari5500@gmail.com\nNew old school instrumental with download link ,Enjoy the beat, I hope you like it!\nPlease comment, rate and subscribe if you like this beat\nI appreciate YOUR support! Peace and respect",
"thumbnails": {
"default": {
"url": "http://ift.tt/2fkilsP",
"width": 120,
"height": 90
},
"medium": {
"url": "http://ift.tt/2hzqOsV",
"width": 320,
"height": 180
},
"high": {
"url": "http://ift.tt/2fjHp3v",
"width": 480,
"height": 360
}
},
"channelTitle": "Ekii020 90` OLDSCHOOL - BOOMBOOMBAP INSTRUMENTALS",
"liveBroadcastContent": "none"
}
},
{
"kind": "youtube#searchResult",
"etag": "\"m2yskBQFythfE4irbTIeOgYYfBU/HXRyGhFaD4Cn0wvF5XPMJKX5tNQ\"",
"id": {
"kind": "youtube#video",
"videoId": "G7ThqpcuPTI"
},
"snippet": {
"publishedAt": "2017-08-05T23:30:24.000Z",
"channelId": "UCsFmkkSVNgvihycqqtjSXgA",
"title": "Homeless (Dark Sad Piano Hip Hop Rap Instrumental Beat)",
"description": "http://ift.tt/2hz6U1d wrote a song about a humble man I met yesterday. We spoke for a while. He just wanted to talk. He was homeless.\n\nI cut some pieces from a few jazz recordings and put them behind a simple piano riff.\n\nThis music has a lot of feeling. Perfectly describes how I felt after our conversation.\n\nTake care.\n\n89 bpm\n\nrightbeatradio.com\http://ntwitter.com/rightbeatradio",
"thumbnails": {
"default": {
"url": "http://ift.tt/2fkimNp",
"width": 120,
"height": 90
},
"medium": {
"url": "http://ift.tt/2hyHsZU",
"width": 320,
"height": 180
},
"high": {
"url": "http://ift.tt/2fj4rao",
"width": 480,
"height": 360
}
},
"channelTitle": "Right Beat Radio",
"liveBroadcastContent": "none"
}
}
]
}
I would like to generate, say, 10 results, but then only pick 1 (one) random [item] from the response. How can I randomize the results, and only pick one of them from this entire response?
I've seen functions such as shuffle() and array_rand() but these don't seem to be the functions that I need.
I have also seen a snippet that goes like this:
$array = json_decode($JSON, true);
$random_entry = array_rand($array['items'], 1);
$json_data = json_encode($random_entry);
I've tried that, but it returns only strings like "nextPageToken" or "kind", etc.
I would like to pick one entire [item] and grab all of its relevant [snippet] data, such as channelId, title, description, thumbnails, etc.
Can anyone point me in the right direction?
Aucun commentaire:
Enregistrer un commentaire