php构造请求的顺序,PHP CURL请求后端API时(POST), 怎么构造请求数据使请求body里有多个boundary...

请求后端API时, 后端接收的数据格式如下所示:

请求方法: post

请求body:

//part1,content-type:application/json

{

"description": "desdes"

}

//part2,content-type: octet-stream

{

"product_img": octet-stream file,

"config_img ": octet-stream file,

"dopm": octet-stream file

}

从API要求的数据看,php curl发送post数据时, 构造的post请求体要有两个content-type

一个为普通的数据Content-Type: application/json

一个要求为content-type: octet-stream, 二进制流, 主要是图片及其他格式文件转换成流的形式, 传输到API进行保存

平时都是使用curl_setopt($curl, CURLOPT_POSTFIELDS, $body);来设置请求body, 那么现在这样的格式要怎么构造请求体

$header = NULL;

$body = [];

$curl = curl_init();

curl_setopt($curl, CURLOPT_URL, $url);

curl_setopt($curl, CURLOPT_POST, true);

curl_setopt($curl, CURLOPT_POSTFIELDS, $body);

curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'POST');

if(!is_null($header)){

curl_setopt($curl, CURLOPT_HTTPHEADER, $header);

}

curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 10);

curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);

curl_setopt($curl, CURLOPT_TIMEOUT, 10);

$curl_get = curl_exec($curl);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值