Ale*_*Per 0 php laravel guzzle guzzle6 guzzlehttp
从 API 文档我有这个curl请求:
curl https://api.example.com/api/Jwt/Token ^
-d Username="asd%40gmail.com" ^
-d Password="abcd1234"
Run Code Online (Sandbox Code Playgroud)
现在我正在尝试使用 Guzzle 库在 Laravel 5.1 中创建该请求,所以我写道:
public function test()
{
$client = new GuzzleHttp\Client();
$res = $client->createRequest('POST','https://api.example.com/api/Jwt/Token', [
'form_params' => [
'Username' => 'asd%40gmail.com',
'Password' => 'abcd1234'
]
]);
$res = json_decode($res->getBody()->getContents(), true);
dd ($res);
}
Run Code Online (Sandbox Code Playgroud)
但我收到此错误:
***ErrorException in Client.php line 126:
Argument 3 passed to GuzzleHttp\Client::request() must be of the type array, string given, called in /home/ibook/public_html/vendor/guzzlehttp/guzzle/src/Client.php on line 87 and defined***
Run Code Online (Sandbox Code Playgroud)
问题是什么,我该如何解决该错误?
ps我也试过
$res = $client->createRequest('POST','https://api.example.com/api/Jwt/Token',
'form_params' => [
'Username' => 'asd%40gmail.com',
'Password' => 'abcd1234'
]);
Run Code Online (Sandbox Code Playgroud)
但后来我得到:
syntax error, unexpected '=>' (T_DOUBLE_ARROW)
Run Code Online (Sandbox Code Playgroud)
您正在调用该createRequest函数而不是request. 这应该有效:
$response = $client->request('POST', 'https://api.example.com/api/Jwt/Token', [
'form_params' => [
'Username' => 'asd%40gmail.com',
'Password' => 'abcd1234'
]
]);
Run Code Online (Sandbox Code Playgroud)
检查文档