php – 将cURL请求转换为Laravel中的Guzzle

前端之家收集整理的这篇文章主要介绍了php – 将cURL请求转换为Laravel中的Guzzle前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
@H_403_4@
我在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请求?

@H_403_4@

解决方法

非常非常容易:

$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.

@H_403_4@ @H_403_4@

猜你在找的Laravel相关文章