【发布时间】:2016-01-02 13:56:47
【问题描述】:
我目前正在使用此代码在 jQuery 中通过 GET 请求进行搜索:
$.get("https://www.googleapis.com/youtube/v3/videos?id=c8a3_9ecqBA&key=MYAPIKEY&part=snippet,contentDetails", function(data) {
console.log(data);
});
并且数据在控制台中正确返回。但我想在 PHP 中做同样的事情(使用 AJAX),所以我一直在尝试:
// 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;
但反应是:
{
"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."
}
}
如何解决?
【问题讨论】:
-
这是我的公共 API 密钥,并且被配置为仅在配置的域上工作,所以,我认为分享这个没有问题...
-
你可以用
CURLOPT_REFERER设置标题的Referer -
它工作了@Federico。谢谢兄弟!做出回答,以便我将其标记为已解决
标签: php curl youtube youtube-api youtube-data-api