gpt4 book ai didi

php - Google URL Shortener - 未指定 HTTP Referer

转载 作者:行者123 更新时间:2023-12-02 17:24:04 24 4
gpt4 key购买 nike

我有 URL 缩短脚本,在我将网站引荐来源网址添加到 API 控制台中的 KEY 限制之前一直运行良好(我必须这样做)。现在我不返回短网址。我收到以下错误:

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]');

关于php - Google URL Shortener - 未指定 HTTP Referer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40275456/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com