php - 从YouTube API JSON响应(PHP)中随机选择一个视频

标签 php json random youtube youtube-data-api

假设我使用YouTube API v3进行了简单的youtube搜索,并且得到了如下响应:

{
 "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": "https://i.ytimg.com/vi/gpZvuZEiINA/default.jpg",
      "width": 120,
      "height": 90
     },
     "medium": {
      "url": "https://i.ytimg.com/vi/gpZvuZEiINA/mqdefault.jpg",
      "width": 320,
      "height": 180
     },
     "high": {
      "url": "https://i.ytimg.com/vi/gpZvuZEiINA/hqdefault.jpg",
      "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://rightbeatradio.com/product/homeless/\n\nI 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\ntwitter.com/rightbeatradio",
    "thumbnails": {
     "default": {
      "url": "https://i.ytimg.com/vi/G7ThqpcuPTI/default.jpg",
      "width": 120,
      "height": 90
     },
     "medium": {
      "url": "https://i.ytimg.com/vi/G7ThqpcuPTI/mqdefault.jpg",
      "width": 320,
      "height": 180
     },
     "high": {
      "url": "https://i.ytimg.com/vi/G7ThqpcuPTI/hqdefault.jpg",
      "width": 480,
      "height": 360
     }
    },
    "channelTitle": "Right Beat Radio",
    "liveBroadcastContent": "none"
   }
  }
 ]
}

我想生成10个结果,但是只能从响应中选择1(一个)随机[item]。如何才能随机化结果,并从整个响应中仅选择其中之一?

我见过shuffle()array_rand()之类的函数,但是这些似乎并不是我需要的函数。

我还看到了如下片段:
$array = json_decode($JSON, true);
$random_entry = array_rand($array['items'], 1);
$json_data = json_encode($random_entry);

我已经尝试过了,但是它只返回“nextPageToken”或“kind”等字符串。

我想选择一个完整的[item]并获取其所有相关的[snippet]数据,例如channelId,标题,描述,缩略图等。

谁能指出我正确的方向?

最佳答案

试试这个

$array = json_decode($JSON, true);
$items = $array['items'];
$random_key = array_rand($items, 1);
$json_data = json_encode($items[$random_key]);

array_rand()返回数组键,而不是数组值。

关于php - 从YouTube API JSON响应(PHP)中随机选择一个视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45532702/

相关文章:

php - 从mysql的两个表中检索数据

json - 将 json 文件插入 mongodb 并指定要用作 _id 的键

javascript - JQuery 和 JSON

php - 从命令行执行 php 和 crontab 时权限被拒绝

php - Oneliner 将字符串添加到另一个字符串之前或附加到另一个字符串

php - 需要从mysql数据库显示图片到fpdf

json - 在 HttpConfiguration 实例中的 ASP.NET Web API 应用程序中处理 json pretty-print 参数

java - java中的自定义长度唯一密码

java - 从满足特定条件的列表中选择随机元素的最快方法

c - 我用数组扫描 .dat 文件并打印它,但是当我重新打印它时,它打印出奇怪的数字