如何使用查询字符串参数制作 Guzzle post
How to make a Guzzle post using query string parameters
在 Google Oauth2 实现中,我尝试使用 guzzle 调用为令牌交换授权码。
以下 guzzle 调用工作正常并且 returns 预期值:
$result = $this->client->post(
'https://www.googleapis.com/oauth2/v3/token?code=<authorization_code>&redirect_uri=<redirect_uri>&client_id=<client_id>&client_secret=<client_secret>&grant_type=authorization_code')
->getBody()->getContents();
然而,这似乎是一种装载 post 请求的肮脏方式。
我尝试了以下方法以使其更清洁:
$result = $this->client->post(
'https://www.googleapis.com/oauth2/v3/token',
[
'query' =>
[
'code' => <authorization_code>,
'redirect_uri' => <redirect_uri>,
'client_id' => <client_id>,
'client_secret' => <client_secret>
'grant_type' => 'authorization_code',
]
]
)->getBody()->getContents();
但是第二次调用会生成 Malformed Json
错误消息。
知道我可能做错了什么或如何调试上面示例中生成的最终 url 吗?
你试过使用数组吗http://php.net/json_encode
我在没有 code
参数的情况下尝试过,它成功了。
$client = new \GuzzleHttp\Client();
$response = $client->post('https://www.googleapis.com/oauth2/v3/token', [
'query' => [
'client_id' => '...apps.googleusercontent.com',
'client_secret' => 'secret',
'refresh_token' => 'token',
'grant_type' => 'refresh_token'
]
]);
$token = $response->getBody()->getContents()
在 Google Oauth2 实现中,我尝试使用 guzzle 调用为令牌交换授权码。
以下 guzzle 调用工作正常并且 returns 预期值:
$result = $this->client->post(
'https://www.googleapis.com/oauth2/v3/token?code=<authorization_code>&redirect_uri=<redirect_uri>&client_id=<client_id>&client_secret=<client_secret>&grant_type=authorization_code')
->getBody()->getContents();
然而,这似乎是一种装载 post 请求的肮脏方式。
我尝试了以下方法以使其更清洁:
$result = $this->client->post(
'https://www.googleapis.com/oauth2/v3/token',
[
'query' =>
[
'code' => <authorization_code>,
'redirect_uri' => <redirect_uri>,
'client_id' => <client_id>,
'client_secret' => <client_secret>
'grant_type' => 'authorization_code',
]
]
)->getBody()->getContents();
但是第二次调用会生成 Malformed Json
错误消息。
知道我可能做错了什么或如何调试上面示例中生成的最终 url 吗?
你试过使用数组吗http://php.net/json_encode
我在没有 code
参数的情况下尝试过,它成功了。
$client = new \GuzzleHttp\Client();
$response = $client->post('https://www.googleapis.com/oauth2/v3/token', [
'query' => [
'client_id' => '...apps.googleusercontent.com',
'client_secret' => 'secret',
'refresh_token' => 'token',
'grant_type' => 'refresh_token'
]
]);
$token = $response->getBody()->getContents()