【问题标题】:Strange Youtube Search API behaviour奇怪的 Youtube 搜索 API 行为
【发布时间】:2017-02-12 13:38:31
【问题描述】:

使用 youtube 数据 ip 时,我无法检索视频 ID。我可以使用所有其他字段,但访问 videoId 字段时出错。下列: https://www.googleapis.com/youtube/v3/search?key={api_key}&channelId=UCXuqSBlHAE6Xw-yeJA0Tunw&part=snippet,id&order=date&maxResults=2

返回

{
"kind": "youtube#searchListResponse",
"etag": "\"I_8xdZu766_FSaexEaDXTIfEWc0/ycDRkf40ZrOgEHl0cz-4b60EIFc\"",
"nextPageToken": "CAIQAA",
"regionCode": "GB",
"pageInfo": {
 "totalResults": 3510,
 "resultsPerPage": 2
},
"items": [
{
"kind": "youtube#searchResult",
"etag": "\"I_8xdZu766_FSaexEaDXTIfEWc0/HOEmnNQHjX9o0bjBMiW0ipFEyl0\"",
"id": {
"kind": "youtube#video",
"videoId": "dkDq3JJ6IvM"
},
"snippet": {
"publishedAt": "2016-10-04T07:05:26.000Z",
"channelId": "UCXuqSBlHAE6Xw-yeJA0Tunw",
"title": "Best All-around Gaming Laptop? - MSI GS63VR Review",
"description": "The GS63VR from MSI is our first GTX 1060-equipped gaming laptop. And man, are we impressed... TunnelBear message: TunnelBear is the easy-to-use VPN ...",
"thumbnails": {
 "default": {
  "url": "https://i.ytimg.com/vi/dkDq3JJ6IvM/default.jpg",
  "width": 120,
  "height": 90
 },
 "medium": {
  "url": "https://i.ytimg.com/vi/dkDq3JJ6IvM/mqdefault.jpg",
  "width": 320,
  "height": 180
 },
 "high": {
  "url": "https://i.ytimg.com/vi/dkDq3JJ6IvM/hqdefault.jpg",
  "width": 480,
  "height": 360
 }
},
"channelTitle": "LinusTechTips",
"liveBroadcastContent": "none"
}
},
{
"kind": "youtube#searchResult",
"etag": "\"I_8xdZu766_FSaexEaDXTIfEWc0/qXo91IDtZrS-0G5AOWB--t3pR4Y\"",
"id": {
"kind": "youtube#video",
"videoId": "g7UX0nP70E0"
},
"snippet": {
"publishedAt": "2016-10-03T06:50:31.000Z",
"channelId": "UCXuqSBlHAE6Xw-yeJA0Tunw",
"title": "The best retro gaming experience ever?",
"description": "Controllers are truly a dime a dozen nowadays... But do these retro-inspired controllers, and their wireless adapters, stand out from the pack? iFixit link: Use offer ...",
"thumbnails": {
 "default": {
  "url": "https://i.ytimg.com/vi/g7UX0nP70E0/default.jpg",
  "width": 120,
  "height": 90
 },
 "medium": {
  "url": "https://i.ytimg.com/vi/g7UX0nP70E0/mqdefault.jpg",
  "width": 320,
  "height": 180
 },
 "high": {
  "url": "https://i.ytimg.com/vi/g7UX0nP70E0/hqdefault.jpg",
  "width": 480,
  "height": 360
 }
},
"channelTitle": "LinusTechTips",
"liveBroadcastContent": "none"
}
}
]
}

现在如果我 $videos = json_decode($result)

foreach($videos->items as $video)
    {
        print_r($video->id->videoId);

    }

我收到一个错误

   [ErrorException]
   Undefined property: stdClass::$videoId

然而 print_r($video->id->kind) 返回想要的结果

print_r($video->id) 也返回:

   stdClass Object
   (
   [kind] => youtube#video
   [videoId] => dkDq3JJ6IvM
   )

【问题讨论】:

  • 不管怎样,您的代码在ideone.com 上运行良好。
  • 我找到了解决办法,我必须json_decode(json_encode(json_decode('YoutubeAPIURL))))

标签: php json youtube-data-api


【解决方案1】:

原来我上面的评论不是解决方案。

实际上,出于某种原因,foreach 循环是问题所在:

 foreach($videos->items as $video)
    {
        print_r($video->id->videoId);
    }

这不起作用并导致错误。只需将 foreach 循环中的 $video 替换为不同的单词(在本例中为 item)即可解决:

foreach($videos->items as $item)
    {
        print_r($item->id->videoId);
    }

【讨论】:

    猜你喜欢
    • 2016-08-11
    • 1970-01-01
    • 2015-09-25
    • 1970-01-01
    • 2018-05-22
    • 2017-01-09
    • 1970-01-01
    • 1970-01-01
    • 2019-05-19
    相关资源
    最近更新 更多