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 curl
When 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 curl
When 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 usedCURLFile
To 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.tcp
The 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