Home  >  Article  >  Backend Development  >  http - When PHP CURL requests the backend API (POST), how to structure the request data so that there are multiple boundaries in the request body?

http - When PHP CURL requests the backend API (POST), how to structure the request data so that there are multiple boundaries in the request body?

WBOY
WBOYOriginal
2016-08-04 09:19:472030browse

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

<code class="bash">请求方法: 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 

}
</code>

From the data required by the API, when php curl sends post data, the constructed post request body must have two content-type

One is ordinary dataContent-Type: application/json

One requirement is content-type: octet-stream, binary stream, mainly converting pictures and other format files into stream form, and transmitting them to API for saving

We usually use curl_setopt($curl, CURLOPT_POSTFIELDS, $body); to set the request body, so how to construct the request body in this format

<code class="php">
$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);
</code>

Reply content:

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

<code class="bash">请求方法: 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 

}
</code>

From the data required by the API, when php curl sends post data, the constructed post request body must have two content-type

One is ordinary dataContent-Type: application/json

One requirement is content-type: octet-stream, binary stream, mainly converting pictures and other format files into stream form, and transmitting them to API for saving

Usually we use curl_setopt($curl, CURLOPT_POSTFIELDS, $body); to set the request body, so how to construct the request body in this format

<code class="php">
$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);
</code>

It is true that CURLFile is used to convert the file into a stream form, but when I was processing it above, the request timeout was too short, causing the data stream to be sent before the data stream was sent, and the tcp link was broken,

It is recommended to set the timeout to 10 seconds when making general CURL request APIs. When uploading files takes too much time, increase the link time and timeout

CURLOPT_FOLLOWLOCATION , CURLOPT_TIMEOUT

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

-Content-Type: application/json: json_encode
-content-type: octet-stream:
php>5.6

<code>$file_data = array('image' => new \CURLFile(realpath($source)));</code>

php<=5.5

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

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn