问题是Google翻译API
我设置了一个项目并开启了结算(在账户上有一些钱)
我为服务器应用创建了一个密钥(也尝试使用浏览器密钥)并添加了我拥有的所有3个IP(家庭和2台服务器)
我看到的内容:
它在apis-explorer和浏览器地址栏中有效(https://www.googleapis.com/language/translate/v2/detect?q=an%20english%20text&key=MY_KEY)
如果我尝试从PHP代码获取相同的URL,则返回403错误:
$ apiKey ='MY_KEY';
$ url ='https://www.googleapis.com/language/translate/v2/detect?q=an%20english%20text&key='。 $ apiKey。 '';
$ handle = curl_init($ url);
curl_setopt($ handle,CURLOPT_RETURNTRANSFER,true);
$ response = curl_exec($ handle);
curl_close($处理);
print_r(json_decode($ response,true));
结果:
Array (
[error] => Array (
[errors] => Array (
[0] => Array (
[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.
))
我检查了IP数十次,尝试使用浏览器密钥和允许的URL作为引用。 出于想法。
答案 0 :(得分:2)
感谢管理员,他们帮助弄明白了。 默认情况下 - 使用我们的ipv6地址curl ...
所以我们在这里有三个选择:
curl_setopt($ handle,CURLOPT_IPRESOLVE,CURL_IPRESOLVE_V4);
将ipv6地址添加到允许列表
关闭服务器的ipv6
我必须更加专注并记住ipv6: - )