我有我认为正确编写的代码,但每当我尝试调用它时,我都会得到谷歌拒绝的许可。
file_get_contents(https://www.googleapis.com/urlshortener/v1/url): failed to open stream: HTTP request failed! HTTP/1.0 403 Forbidden
这不是速率限制或任何东西,因为我目前使用过的零...
我原以为这是由于 API 密钥不正确,但我尝试过多次重置它。首次应用 API 时没有停机时间吗?
还是我错过了标题设置或其他同样小的东西?
public function getShortUrl()
{
$longUrl = "http://example.com/";
$apiKey = "MY REAL KEY IS HERE";
$opts = array(
'http' =>
array(
'method' => 'POST',
'header' => "Content-type: application/json",
'content' => json_encode(array(
'longUrl' => $longUrl,
'key' => $apiKey
))
)
);
$context = stream_context_create($opts);
$result = file_get_contents("https://www.googleapis.com/urlshortener/v1/url", false, $context);
//decode the returned JSON object
return json_decode($result, true);
}