微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

基本上,我想列出YouTube频道的最新上传内容,但是我在使用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)的输出如下:

    {
 "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秒以上的错误.

有什么想法吗?

问候,

谢谢!

解决方法:

搞砸了

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

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐