Reputation: 47
I'm getting started trying to understand Guzzle but one of my requests keeps returning an error, even though the exact same request when done using CURL works just fine.
I have a refresh_token and want to get an access_token from WEB API.
The Guzzle request that results in an error:
$refresh_token = '<token>';
$client = new GuzzleHttp\Client(['headers' => ['Content-Type' => 'application/x-www-form-urlencoded']]);
$response = $client->request('POST', 'https://foo.bar/secure/token', [
'query' => ['grant_type' => 'refresh_token','refresh_token' => $refresh_token]
]);
echo $response->getStatusCode();
echo $response->getBody();
Fatal error: Uncaught exception 'GuzzleHttp\Exception\ClientException' with message 'Client error: resulted in a 400 Bad Request
response' in vendor/guzzlehttp/guzzle/src/Exception/RequestException.php:113
This CURL request works just fine:
$refresh_token = '<token>';
$params=['grant_type'=>'refresh_token',
'refresh_token'=>$refresh_token
];
$headers = [
'POST /secure/token HTTP/1.1',
'Content-Type: application/x-www-form-urlencoded'
];
$curlURL='https://foo.bar/secure/token';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$curlURL);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS,http_build_query($params));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_VERBOSE, true);
$curl_res = curl_exec($ch);
if($curl_res) {
$server_output = json_decode($curl_res);
}
var_dump($curl_res);
I hope for your help. Here's the Guzzle debug that was printed out in the browser.
Request Method: GET
Status Code: 200 OK
Remote Address: 87.236.19.237:80
Referrer Policy: no-referrer-when-downgrade
Connection: keep-alive
Content-Encoding: gzip
Content-Type: text/html
Date: Wed, 21 Aug 2019 15:46:25 GMT
Keep-Alive: timeout=30
Server: nginx-reuseport/1.13.4
Transfer-Encoding: chunked
Vary: Accept-Encoding
X-Powered-By: PHP/5.6.38
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3
Accept-Encoding: gzip, deflate
Accept-Language: ru-RU,ru;q=0.9,en-US;q=0.8,en;q=0.7
Cache-Control: max-age=0
Connection: keep-alive
DNT: 1
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36
Upvotes: 1
Views: 1171
Reputation: 47
This is correct! Tnx Jonnix!
$response = $client->request('POST', 'https://sso.tinkoff.ru/secure/token', [
'form_params' => [
'grant_type' => 'refresh_token',
'refresh_token' => $refresh_token
]
]);
Upvotes: 0
Reputation: 109
Your request becomes GET probably because of the "query" parameter.
Use form_params instead of query.
See the documentation.
http://docs.guzzlephp.org/en/stable/request-options.html#form-params
Upvotes: 1