Convert cURL request to Guzzle in Laravel

I have a curl request, as in Codeigniter:

$order = [
  'index' => 'Value',
  'index2' => 'Value2'
];

$this->curl->create($this->base_url.'order/');
$this->curl->http_login($creds['username'], $creds['password']);
$this->curl->ssl(TRUE, 2, 'certificates/certificate.pem');
$this->curl->option(CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Accept: application/json'));
$this->curl->option(CURLOPT_FAILONERROR, FALSE);

$this->curl->post(json_encode($order));
$data   = $this->curl->execute();

Now I need to send the same request to Laravel, where I use Guzzle. How can I convert this to a Guzzle request?

+4
source share
1 answer

Very, very easy:

$client = new GuzzleHttp\Client(['base_uri' => $this->base_url]);

$response = $client->request('POST', 'order/', [
  'form_params' => $order,
  'headers' => [
    'Content-Type' => 'application/json',
    'Accept' => 'application/json'
  ],
  'auth' => [$creds['username'], $creds['password']],
  'http_errors' => false,
  'verify' => 'certificates/certificate.pem'
]);

echo $response->getBody();

Note that this has nothing to do with Laravel, it's just Guzzle. Laravel has no effect on the GUZLE API.

0
source

Source: https://habr.com/ru/post/1661406/


All Articles