gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 05:27:47 28 4
gpt4 key购买 nike

我目前正在使用此代码在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."
}
}

如何解决?

最佳答案

错误的原因是请求需要启用引用。您可以通过将CURLOPT_REFERER选项添加到curl_setopt_array配置中来进行设置。

关于php - YouTube视频使用 curl 返回错误使用API​​ V3搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32956588/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com