将cURL请求转换为Laravel中的Guzzle

时间:2016-11-21 05:08:32

标签: php laravel curl guzzle

我在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();

现在我需要在Laravel中发出相同的请求,我正在使用Guzzle。如何将其转换为Guzzle请求?

1 个答案:

答案 0 :(得分:0)

非常非常容易:

$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();

请注意,这与Laravel无关,它只是Guzzle。 Laravel不会以任何方式影响Guzzle API。