您好,欢迎访问一九零五行业门户网

http - 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);

回复内容: 请求后端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);

确实是用curlfile来将文件转换为流形式, 只是上面我在处理时, 请求超时时间太短, 导致数据流还没发送完成, 该tcp链接就断了,
建议在一般curl请求api时, 超时时间设置为10秒。 而向文件上传耗时太多时, 增大链接时间和超时时间
curlopt_followlocation , curlopt_timeout
$header = null;$body = [ 'img' => new curlfile('imagepath', 'octet-stream', 'file_name')];$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);}//设置链接超时时间为1分钟curl_setopt($curl, curlopt_connecttimeout, 60);curl_setopt($curl, curlopt_followlocation, true); curl_setopt($curl, curlopt_timeout, 60);$curl_get = curl_exec($curl);
-content-type: application/json: json_encode
-content-type: octet-stream:
php>5.6
$file_data = array('image' => new \curlfile(realpath($source)));
php
$file_data = array('image'=> '@' . realpath($source));//
其它类似信息

推荐信息