Guzzle 将正文编码为 Json,但我想要表单数据
Guzzle encodes body to Json, but I want form data
我很想 post 想 Googles oAuth server
做一些验证。 Google 确实想要表单数据中的此信息 (Content-Type: application/x-www-form-urlencoded
),但我的 guzzle 客户似乎坚持(据我所知)制作正文 JSON。我正在使用 Guzzle 4。*
我已经将我的 URL 更改为 PostCatcher.io url
,所以我可以看到结果(因为我一辈子都不知道如何查看实际的原始 HTTP request that guzzle spit out),看起来 JSON 出来了。
我的代码(我现在正在使用测试 url):
$client = new GuzzleClient();
$url = "https://www.googleapis.com/" . "oauth2/v3/token";
$test_url = 'http://postcatcher.in/catchers/55602457b92ce203000032ae';
$request = $client->createRequest('POST', $test_url);
$request->setHeader('Content-Type', 'application/x-www-form-urlencoded'); //should be redundant
$body = $request->getBody();
$body->setField('code', $code);
$body->setField('client_id', $this->client_id);
$body->setField('client_secret', $this->client_secret);
$body->setField('redirect_url', $this->redicrect_url);
$body->setField('grant_type', $this->grant_type);
try {
$response = $client->send($request);
$result = $response->getBody();
return $result;
} catch (\Exception $exc) {
return $exc->getCode() . ' ' . $exc->getMessage();
}
The documentation says this should be enough. What am I missing ?
我使用 Guzzle 5.2 完成了同样的事情:
$request = $this->createRequest(
$method,
$uri,
['body' => $php_array_of_values ]
);
$response = $this->send($request);
我发现了问题。我确实升级到 Guzzle 5.* 但我怀疑这实际上不是解决方案。
我只需要看看回复内容。所以在我添加的调用的异常子句中:
if ($exc->hasResponse()) {
return $exc->getResponse()->json();
}
Google 给了我一个明确的错误信息。错误消息是 "Missing parameter: redirect_uri"。那是因为我写了 url
而不是 uri
。
已修复,现在一切正常。
我很想 post 想 Googles oAuth server
做一些验证。 Google 确实想要表单数据中的此信息 (Content-Type: application/x-www-form-urlencoded
),但我的 guzzle 客户似乎坚持(据我所知)制作正文 JSON。我正在使用 Guzzle 4。*
我已经将我的 URL 更改为 PostCatcher.io url
,所以我可以看到结果(因为我一辈子都不知道如何查看实际的原始 HTTP request that guzzle spit out),看起来 JSON 出来了。
我的代码(我现在正在使用测试 url):
$client = new GuzzleClient();
$url = "https://www.googleapis.com/" . "oauth2/v3/token";
$test_url = 'http://postcatcher.in/catchers/55602457b92ce203000032ae';
$request = $client->createRequest('POST', $test_url);
$request->setHeader('Content-Type', 'application/x-www-form-urlencoded'); //should be redundant
$body = $request->getBody();
$body->setField('code', $code);
$body->setField('client_id', $this->client_id);
$body->setField('client_secret', $this->client_secret);
$body->setField('redirect_url', $this->redicrect_url);
$body->setField('grant_type', $this->grant_type);
try {
$response = $client->send($request);
$result = $response->getBody();
return $result;
} catch (\Exception $exc) {
return $exc->getCode() . ' ' . $exc->getMessage();
}
The documentation says this should be enough. What am I missing ?
我使用 Guzzle 5.2 完成了同样的事情:
$request = $this->createRequest(
$method,
$uri,
['body' => $php_array_of_values ]
);
$response = $this->send($request);
我发现了问题。我确实升级到 Guzzle 5.* 但我怀疑这实际上不是解决方案。
我只需要看看回复内容。所以在我添加的调用的异常子句中:
if ($exc->hasResponse()) {
return $exc->getResponse()->json();
}
Google 给了我一个明确的错误信息。错误消息是 "Missing parameter: redirect_uri"。那是因为我写了 url
而不是 uri
。
已修复,现在一切正常。