How to construct request data when http-PHPCURL requests the backend API (POST) so that the request body contains multiple boundary

Source: Internet
Author: User
When a backend API is requested, the data format received by the backend is as follows: {code ...} according to the data required by the API, when phpcurl sends post data, the constructed post request body must have two content-types: applicationjson must be cont... when a backend API is requested, the data format received by the backend is as follows:

Request method: post request 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}

According to the data required by the API,php curlWhen sending post data, the constructed post request body must have twocontent-type

A common dataContent-Type: application/json

One requirement iscontent-type: octet-stream, Binary stream, which is mainly used to convert images and other format files into streams and transfer them to the API for storage.

It is usually usedcurl_setopt($curl, CURLOPT_POSTFIELDS, $body);To set the request body. how to construct the request body in this format?

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

Reply content:

When a backend API is requested, the data format received by the backend is as follows:

Request method: post request 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}

According to the data required by the API,php curlWhen sending post data, the constructed post request body must have twocontent-type

A common dataContent-Type: application/json

One requirement iscontent-type: octet-stream, Binary stream, which is mainly used to convert images and other format files into streams and transfer them to the API for storage.

It is usually usedcurl_setopt($curl, CURLOPT_POSTFIELDS, $body);To set the request body. how to construct the request body in this format?

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

It is actually usedCURLFileTo convert the file to the stream format, but the request timeout is too short during the above processing, resulting in the data flow has not been completely sent.tcpThe link is broken,

We recommend that you set the timeout value to 10 seconds when CURL requests APIs. When uploading a file takes too much time, the link time and timeout time are increased.

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) ;}// set the link timeout time to 1 minute 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.