youtube API V3(片段,大拇指,标题)在网页上显示视频的问题

时间:2018-04-09 08:29:31

标签: php google-api youtube-api youtube-data-api

我的api Key有问题。我在尝试请求时: 我得到一个空数组

     {
     "kind": "youtube#videoListResponse",
     "etag": "\"RmznBCICv9YtgWaaa_nWDIH1_GM/Rk41fm-2TD0VG1yv0-bkUvcBi9s\"",
    "pageInfo": {
      "totalResults": 0,
      "resultsPerPage": 0
     },
    "items": [
     ]
    }

我的密钥似乎很好,在视频在网站上显示良好之前,但现在我不知道为什么在尝试从YouTube获取数据的请求时没有响应。

我有这个方法:

            public function getYoutube($id) 
        {
            $youtube ='https://www.googleapis.com/youtube/v3/videos?id='.$id.'&fields=items%28snippet%28title,description,%20thumbnails%29%29&part=snippet&key=my_key';


            $json =  file_get_contents($youtube);
            $itemList = json_decode($json);

            $this->video = $itemList->items[0]; 
            $this->id = $id; 

            if(!$json)
                return false ;
            else
                return $this->video ;
        }

1 个答案:

答案 0 :(得分:0)

听起来file_get_contents失败了。

  1. 检查您的php.ini以查看是否已禁用allow_url_fopen
  2. 将来,请始终检查可以返回故障的功能的结果,例如

    $json =  file_get_contents($youtube);
    if ( false === $json ) {
        // it failed. do something.
    }
    

    如果失败,您还可以检查服务器的错误文件,它应该有更多信息。