data request working with cURL in PHP?
I'm having a difficult time POSTING to an API with PHP cURL. I feel I've tried every combination. My script is connecting but the issue is somewhere within the payload.
Here is the required request according to the documentation.
--85b890d4-4faf-4261-bcbb-187c4dddcbc6 Content-Type: application/vnd.emc.ax+json; charset=utf-8 Content-Disposition: form-data; name=data
{
"Name": "batch name",
"Description": "batch description",
"Private": false
}
--85b890d4-4faf-4261-bcbb-187c4dddcbc6 Content-Type: application/bin Content-Disposition: form-data; name=bin; filename="test.pdf"; filename*=utf-8''test.pdf
//bin file content
--85b890d4-4faf-4261-bcbb-187c4dddcbc6 Content-Type: application/bin Content-Disposition: form-data; name=annotation; filename="0100002m.ano"; filename*=utf-8''0100002m.ano
//annotation file content
--85b890d4-4faf-4261-bcbb-187c4dddcbc6 Content-Type: application/bin Content-Disposition: form-data; name=text; filename="test.txt"; filename*=utf-8''test.txt
//ocr file content
--85b890d4-4faf-4261-bcbb-187c4dddcbc6--
Here is my code
$file = file_get_contents("tiff/test.TIF");
$curl = curl_init();
$data = array(
'Content-Type: application/vnd.emc.ax+json; charset=utf-8 Content Disposition: form-data; name=data',
'{"Name":"Here is my Name","Description":"Testing This","Private":false}',
chunk_split(base64_encode($file))
);
curl_setopt_array($curl, array(
CURLOPT_URL =>"*********************************",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $d,
CURLOPT_HTTPHEADER => array(
"Authorization: **********************************",
"Content-Type: multipart/form-data; boundary=85b890d4-4faf-4261-bcbb-187c4dddcbc6",
),
));
curl_close($curl);
My code produces this error:
{"ErrorCode":9999,"Message":"Unexpected end of MIME multipart stream. MIME multipart message is not complete.","InnerException":null}
will someone please help me?
I did an implementation using pure CURL uploading files with multipart. Try this:
curl -X POST
http://youhosturl/path/to/you/endpoint
-H 'content-type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW'
-F 'my_file=@/Users/MyUser/Desktop/any-image.jpg'
This worked for me
链接地址: http://www.djcxy.com/p/48902.html上一篇: 如何使用PHP / cURL模拟浏览器表单POST方法
下一篇: 数据请求在PHP中使用cURL?