我正在尝试将 Google URL 缩短 API 与 PHP 结合使用:
$apiKey = 'ABC';
$url = 'http://www.stackoverflow.com/';
$postData = array('longUrl' => $url);
$jsonData = json_encode($postData);
$curlObj = curl_init();
curl_setopt($curlObj, CURLOPT_URL, 'https://www.googleapis.com/urlshortener/v1/url?key=' . $apiKey);
curl_setopt($curlObj, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curlObj, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curlObj, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($curlObj, CURLOPT_HEADER, 0);
curl_setopt($curlObj, CURLOPT_HTTPHEADER, array("Content-Type: application/json; charset=utf-8","Accept:application/json, text/javascript, */*; q=0.01"));
curl_setopt($curlObj, CURLOPT_POST, 1);
curl_setopt($curlObj, CURLOPT_POSTFIELDS, $jsonData);
$result = curl_exec($curlObj);
但我收到以下错误消息:
{
"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."
}
我已经检查了以下内容:
- 设置引荐来源网址(http 和 https)
- API 控制台注册了所有请求,但遗憾的是作为“客户端错误”而不是“成功”
每当用户访问该页面时就会触发该脚本。
我真的很感谢任何帮助我解决这个恼人问题的提示。我似乎“在那里”找不到任何解决方案。