2017-05-28 69 views
0

从API文档我有这样的要求curlLaravel狂饮参数

curl https://api.example.com/api/Jwt/Token^
     -d Username="asd%40gmail.com"^
     -d Password="abcd1234" 

现在我试图创建一个使用狂饮库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); 
} 

但我得到这个错误:

***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*** 

问题是什么,如何解决这个错误?

p.s.我也试过

$res = $client->createRequest('POST','https://api.example.com/api/Jwt/Token', 

    'form_params' => [ 
     'Username' => 'asd%40gmail.com', 
     'Password' => 'abcd1234' 

      ]); 

但后来我得到:

syntax error, unexpected '=>' (T_DOUBLE_ARROW) 

回答

3

您呼叫的createRequest功能,而不是request。这应该工作:

$response = $client->request('POST', 'https://api.example.com/api/Jwt/Token', [ 
    'form_params' => [ 
     'Username' => 'asd%40gmail.com', 
     'Password' => 'abcd1234' 
    ] 
]); 

检查documentation