【发布时间】:2015-03-18 13:34:29
【问题描述】:
我正在尝试将 Google URL Shorter 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 控制台注册了所有请求,但遗憾的是作为“客户端错误”而不是“成功”
每当用户访问该页面时都会触发该脚本。
我非常感谢任何可以帮助我解决这个烦人问题的提示。我似乎没有在“外面”找到任何解决方案。
【问题讨论】:
-
感谢您的评论。是的,我知道。问题是我已经注册了拨打电话的 IP,但仍然收到此错误。
标签: php google-api