正在开发包含大量表单的Web应用程序。 我正在使用谷歌的公共链接生成API。
<?php
include 'config.php';
include 'php/lib/google_api/googleURL_shortener.php';
?>
<html>
<head></head>
<body>
<?php
try {
$key = "AIzaSyCzDa3nhryO23Aa-0VlxasYkZ-PPqeWfrY";
$googleApi = new GoogleURL($key);
$publicLink = '';
$url = BASE_URL.'form_operation/index.php? id=54ccb4db64363d9c1100002d';
$publicLink = $googleApi->encode($url);
echo $publicLink;
} catch (Exception $ex) {
echo $ex;
}
?>
</body>
</html>
我已经包含了google shortener API库api库的代码就是这个
<?php
class GoogleURL
{
private $apiURL = 'https://www.googleapis.com/urlshortener/v1/url';
function __construct($apiKey)
{
$this->apiURL = $this->apiURL . '?key=' . $apiKey;
}
public function encode($url)
{
$data = $this->cURL($url, true);
return isset($data->id) ? $data->id : 'No data present' ;
}
public function decode($url)
{
$data = $this->cURL($url, false);
return isset($data->analytics->allTime->shortUrlClicks) ? $data- >analytics->allTime->shortUrlClicks : 0 ;
}
private function cURL($url, $post = true)
{
$ch = curl_init();
if ($post) {
curl_setopt( $ch, CURLOPT_URL, $this->apiURL );// cURL defined
curl_setopt( $ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json') );
curl_setopt( $ch, CURLOPT_POST, true );
curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode(array('longUrl' => $url)) );
}
else {
curl_setopt( $ch, CURLOPT_URL, $this->apiURL . '&projection=FULL&shortUrl=' . $url );
}
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false );
$json = curl_exec($ch);
curl_close($ch);
return (object) json_decode($json);
}
}
?>
每次从localhost调用API时,我都会得到null输出。
我收到了以下回复
stdClass Object
(
[error] => stdClass Object
(
[errors] => Array
(
[0] => stdClass Object
(
[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.
)
)
可以做些什么?