加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

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

发布时间:2020-12-14 19:37:39 所属栏目:大数据 来源:网络整理
导读:我在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.p
我在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请求?

解决方法

非常非常容易:

$client = new GuzzleHttpClient(['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.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读