我想使用 Google URL 缩短 API。现在,我需要向 Google API 发送一个 JSON POST 请求。
我在 PHP 中使用 Guzzle 6.2。
以下是我尝试过的:
$client = new GuzzleHttp\Client();
$google_api_key = 'AIzaSyBKOBhDQ8XBxxxxxxxxxxxxxx';
$body = '{"longUrl" : "http://www.google.com"}';
$res = $client->request('POST', 'https://www.googleapis.com/urlshortener/v1/url', [
'headers' => ['Content-Type' => 'application/json'],
'form_params' => [
'key'=>$google_api_key
],
'body' => $body
]);
return $res;
但是它返回以下错误:
Client error: `POST https://www.googleapis.com/urlshortener/v1/url` resulted in a `400 Bad Request` response:
{
"error": {
"errors": [
{
"domain": "global",
"reason": "parseError",
"message": "Parse Error"
}
(truncated...)
希望能得到任何帮助。我已经阅读了 Guzzle 文档和其他很多资源,但都没有帮助!
$res->getBody()->getContents()
来查看它。 - Alexey Shokov