2017-05-28 99 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