php - 使用 PHP for 和 foreach 循环从 JSON 数据中检索视频信息时遇到问题 - YouTube API 3

标签 php json youtube-api

基本上我想列出 YouTube channel 的最后上传,但我在使用 for 循环时遇到了问题,或者我也缺少 foreach 的使用。

我尝试了以下方法:

function last_uploads() {
for($i = 0; $i < 20; ){
        error_reporting(E_ALL);
        $url =      'https://www.googleapis.com/youtube/v3/search?order=date&part=snippet&channelId=UCBR8-60-B28hp2BmDPdntcQ&maxResults=3&key={key}&type=video';
        $json = file_get_contents($url);
        $json_data = json_decode($json, false);
        $id = $json_data->items[0]->id->videoId;
                echo '<iframe id="ytplayer" type="text/html" width="auto" height="auto" 
                src="http://www.youtube.com/embed/' . $id . '?rel=0&showinfo=1"
                frameborder="0" allowfullscreen></iframe><br class="clear" />';
}
        $i++;
        break;
}

提要 url ( $url ) 的输出如下:

    {
 "kind": "youtube#searchListResponse",
 "etag": "\"IHLB7Mi__JPvvG2zLQWAg8l36UU/sa0AWnsYjVk9MPM3sjMlLcrfFxU\"",
 "nextPageToken": "CAMQAA",
 "pageInfo": {
  "totalResults": 219,
  "resultsPerPage": 3
 },
 "items": [
  {
   "kind": "youtube#searchResult",
   "etag": "\"IHLB7Mi__JPvvG2zLQWAg8l36UU/2QHwn2uxHHlB9OQ9ViQw0LdnQwI\"",
   "id": {
    "kind": "youtube#video",
    "videoId": "AbqT_ubkT0Y"
   },
   "snippet": {
    "publishedAt": "2015-03-03T17:44:56.000Z",
    "channelId": "UCBR8-60-B28hp2BmDPdntcQ",
    "title": "#DearMe - What Advice Would You Give Your Younger Self?",
    "description": "Share your advice by making your own #DearMe GIF at http://youtubedearme.com ** In celebration of International Women's Day, take part in YouTube's global ...",
    "thumbnails": {
     "default": {
      "url": "https://i.ytimg.com/vi/AbqT_ubkT0Y/default.jpg"
     },
     "medium": {
      "url": "https://i.ytimg.com/vi/AbqT_ubkT0Y/mqdefault.jpg"
     },
     "high": {
      "url": "https://i.ytimg.com/vi/AbqT_ubkT0Y/hqdefault.jpg"
     }
    },
    "channelTitle": "YouTube",
    "liveBroadcastContent": "none"
   }
  },
  {
   "kind": "youtube#searchResult",
   "etag": "\"IHLB7Mi__JPvvG2zLQWAg8l36UU/PoM38fElRhJodbFBOt_g1xgJ2RE\"",
   "id": {
    "kind": "youtube#video",
    "videoId": "OUmMAAPX6E8"
   },
   "snippet": {
    "publishedAt": "2015-02-23T14:35:35.000Z",
    "channelId": "UCBR8-60-B28hp2BmDPdntcQ",
    "title": "Introducing the YouTube Kids app",
    "description": "Download YouTube Kids for free on your Android or iOS device. Android: http://goo.gl/SsDTHh iOS: http://goo.gl/P0cikI The YouTube Kids app is designed for ...",
    "thumbnails": {
     "default": {
      "url": "https://i.ytimg.com/vi/OUmMAAPX6E8/default.jpg"
     },
     "medium": {
      "url": "https://i.ytimg.com/vi/OUmMAAPX6E8/mqdefault.jpg"
     },
     "high": {
      "url": "https://i.ytimg.com/vi/OUmMAAPX6E8/hqdefault.jpg"
     }
    },
    "channelTitle": "YouTube",
    "liveBroadcastContent": "none"
   }
  },
  {
   "kind": "youtube#searchResult",
   "etag": "\"IHLB7Mi__JPvvG2zLQWAg8l36UU/-017SY9hNMURE5Slk5j5CU087wE\"",
   "id": {
    "kind": "youtube#video",
    "videoId": "SDdXVOD4llU"
   },
   "snippet": {
    "publishedAt": "2015-01-24T02:59:11.000Z",
    "channelId": "UCBR8-60-B28hp2BmDPdntcQ",
    "title": "Highlights From the YouTube Interview with President Obama",
    "description": "On Jan 22, 2015, President Obama sat down for his first interview after the State of the Union with popular YouTube creators, Bethany Mota, Hank Green and ...",
    "thumbnails": {
     "default": {
      "url": "https://i.ytimg.com/vi/SDdXVOD4llU/default.jpg"
     },
     "medium": {
      "url": "https://i.ytimg.com/vi/SDdXVOD4llU/mqdefault.jpg"
     },
     "high": {
      "url": "https://i.ytimg.com/vi/SDdXVOD4llU/hqdefault.jpg"
     }
    },
    "channelTitle": "YouTube",
    "liveBroadcastContent": "none"
   }
  }
 ]
}

问题是 last_uploads() 返回最后 1 个视频并不断重复,直到出现 120 seconds exceeded 错误。

有什么想法吗?

问候,

谢谢!

最佳答案

只是搞砸了。

  1. 您的 for 循环将无限期执行直到超时,因为 $i++ 在 for 循环之后。 (事实是,甚至不需要 for 循环,因为您正在从 channel 中获取最新的 3 个视频 - 无需执行 20 次)。
  2. 您的代码仅获取返回的第一个视频的 ID。这是 for 循环应该循环遍历结果的地方。

    foreach ( $json_data->items 作为 $item ) { $id = $item->id->videoId; # 打印你的东西

关于php - 使用 PHP for 和 foreach 循环从 JSON 数据中检索视频信息时遇到问题 - YouTube API 3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29772855/

相关文章:

phpcbf 提示缺少 CodeSniffer

json - 编辑大型 JSON 文件

java - json 对象中的类实例字段返回 null

ios - 最近没有在 UIWebView 上使用 iframe 播放器播放 youtube 剪辑

youtube - 压缩的YouTube默认图像

php - 您如何确保一个多词术语可以组合在一起并且不会中断?

php - PHP中如何实现imagecreatefromstring的反向效果?

php - 电子邮件链接中的 PayPal 自定义变量

json - 如何提取在 BigQuery 中存储为字符串的 JSON 数组

javascript - 在 iPhone 上检测 “Done” 以获取 YouTube/Vimeo 视频(退出全屏)