dongzouhe9734 2015-10-05 19:40
浏览 55
已采纳

使用curl返回错误,使用API​​ V3搜索YouTube视频

I'm currently using this code to do a search by GET request in jQuery:

$.get("https://www.googleapis.com/youtube/v3/videos?id=c8a3_9ecqBA&key=MYAPIKEY&part=snippet,contentDetails", function(data) {
    console.log(data);
});

And the data is returned correctly in the console. But I want to do the same thing in PHP (using AJAX), so I've been trying:

// Get cURL resource
$curl = curl_init();

// Set some options - we are passing in a useragent too here
curl_setopt_array($curl, array(
    CURLOPT_RETURNTRANSFER => 1,
    CURLOPT_URL => "https://www.googleapis.com/youtube/v3/videos?q=Eminem&key=MYAPIKEY&part=snippet&type=video&maxResults=20",
    CURLOPT_USERAGENT => "Simple Test"
));

// Send the request & save response to $resp
$resp = curl_exec($curl);

// Close request to clear up some resources
curl_close($curl);

echo $resp;

But the response is:

 {
  "error": {
      "errors": [
           {
              "domain": "usageLimits",
              "reason": "ipRefererBlocked",
              "message": "There is a per-IP or per-Referer restriction configured on your API key and the request does not match these restrictions. Please use the Google Developers Console to update your API key configuration if request from this IP or referer should be allowed.",
              "extendedHelp": "https://console.developers.google.com"
            }
      ],
      "code": 403,
      "message": "There is a per-IP or per-Referer restriction configured on your API key and the request does not match these restrictions. Please use the Google Developers Console to update your API key configuration if request from this IP or referer should be allowed."
      }
 }

How to fix it?

  • 写回答

1条回答 默认 最新

  • duanrang3357 2015-10-05 19:51
    关注

    The reason of the error is that the request need an enabled Referer. You can set it by adding CURLOPT_REFERER options to your curl_setopt_array configuration.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法