0

I have the following CURL Request to an API: curl -X POST -H "X-Api-Key:somekey" --data "name=myname&email=test@test.com" https://api.test.com/accounts

What is the Equivalent script to use in GuzzleHTTP Psr7. I tried the following but didn't work, it didn't receive the $query data array.

$query = [
'name'=>'MyName',
'email'=>'Email@think-ds.com',
];
$client = new Client([
   'base_uri' => 'https://api.test.com/v1/',
   'verify' =>'cert.pem',
]);

$headers=['form_params'=>$query];
try{
    $request = new Request('POST', 'accounts', $headers);           
    $response = $client->send($request);
} catch (\GuzzleHttp\Exception\ClientException $e) {
    die($e->getResponse()->getBody()->getContents()); exit;
}
var_dump($response->getBody());
Marwan
  • 1,802
  • 2
  • 17
  • 22
  • 1
    Based [on this answer](http://stackoverflow.com/questions/34577278/guzzle-not-sending-psr-7-post-body-correctly) you should place the `X-Api-Key` header in `$headers` (not the form params) and then pass the full encoded query string `name=myname&email=test@test.com` in the `$body` parameter which follows `$headers`. I've not used the newer Guzzle APIs to verify that though. – Michael Berkowski May 16 '16 at 10:49
  • Thanks Michael, the answer you mentioned helped me to fix it, although I have made another solution that works also, by send "form_params" as 2nd parameter in send() function: $response = $this->client->send($request, ['form_params'=>$query]); – Marwan May 16 '16 at 14:52
  • 1
    You should post your own complete solution below as an answer @Marwan. – Michael Berkowski May 16 '16 at 14:56

0 Answers0