200字范文,内容丰富有趣,生活中的好帮手!
200字范文 > http – PHP CURL请求后端API时(POST) 怎么构造请求数据使请求body里有多个boundary

http – PHP CURL请求后端API时(POST) 怎么构造请求数据使请求body里有多个boundary

时间:2019-06-28 18:29:56

相关推荐

http – PHP CURL请求后端API时(POST)  怎么构造请求数据使请求body里有多个boundary

后端开发|php教程

php,http

后端开发-php教程

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

layui .net源码下载,ubuntu下优秀软件,爬虫处理excel表,codelg PHP,淮北SEO优化lzw

请求方法: 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

win10源码 网盘下载,vscode切换编码方式,ubuntu的ghos,tomcat8080访问,聚合新闻爬虫,php 输出异常,茂名seo排名哪家质量好,网站代码的图片,绿色品牌化妆品公司官网html模板下载lzw

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

个人简介源码html,ubuntu开机滚字,xpath爬虫语法知识,重构 php,玉树网页seolzw

一个要求为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链接就断了,

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<=5.5

$file_data = array(image=> @ . realpath($source));//<=5.5

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。