私は正しく書かれたコードだと思うものを持っていますが、それを呼び出しようとすると、Google から許可が拒否されます。
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);
}