Google URL 缩短器 - 未指定 HTTP Referer
Google URL Shortener - not specified HTTP Referer
我有 URL 缩短脚本,在我将网站引荐来源网址添加到 API 控制台中的 KEY 限制之前一直运行良好(我必须这样做)。现在我不 return 做空 url。我收到以下错误:
Array ( [error] => Array ( [errors] => Array ( [0] => Array ( [domain]
=> usageLimits [reason] => ipRefererBlocked [message] => The request did not specify any referer. Please ensure that the client is sending
referer or use the API Console to remove the referer restrictions.
[extendedHelp] =>
https://console.developers.google.com/apis/credentials?project=XXXXXXXXX
) ) [code] => 403 [message] => The request did not specify any
referer. Please ensure that the client is sending referer or use the
API Console to remove the referer restrictions. ) )
我的PHP:
<?php
$longurl = "http://example.com";
$api_key_google = "XXXX_API_KEY_XXXXX";
$curl = curl_init('https://www.googleapis.com/urlshortener/v1/url?key='.$api_key_google);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-type:application/json'));
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode(array('longUrl' => $longurl)));
$return = json_decode(curl_exec($curl), true);
curl_close($curl);
print_r($return);
echo $shortDWName = $return['id'];
?>
我在这里错过了什么?提前感谢您的帮助。
尝试添加这一行
curl_setopt($curl, CURLOPT_HEADER, 0);
// should add this line
curl_setopt($ch, CURLOPT_REFERER, '[your restriction domain]');
我有 URL 缩短脚本,在我将网站引荐来源网址添加到 API 控制台中的 KEY 限制之前一直运行良好(我必须这样做)。现在我不 return 做空 url。我收到以下错误:
Array ( [error] => Array ( [errors] => Array ( [0] => Array ( [domain] => usageLimits [reason] => ipRefererBlocked [message] => The request did not specify any referer. Please ensure that the client is sending referer or use the API Console to remove the referer restrictions. [extendedHelp] => https://console.developers.google.com/apis/credentials?project=XXXXXXXXX ) ) [code] => 403 [message] => The request did not specify any referer. Please ensure that the client is sending referer or use the API Console to remove the referer restrictions. ) )
我的PHP:
<?php
$longurl = "http://example.com";
$api_key_google = "XXXX_API_KEY_XXXXX";
$curl = curl_init('https://www.googleapis.com/urlshortener/v1/url?key='.$api_key_google);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-type:application/json'));
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode(array('longUrl' => $longurl)));
$return = json_decode(curl_exec($curl), true);
curl_close($curl);
print_r($return);
echo $shortDWName = $return['id'];
?>
我在这里错过了什么?提前感谢您的帮助。
尝试添加这一行
curl_setopt($curl, CURLOPT_HEADER, 0);
// should add this line
curl_setopt($ch, CURLOPT_REFERER, '[your restriction domain]');