我需要使用HTTPS上传一个ZIP文件,这只能通过Linux cURL命令完成。我不明白在PHP cURL请求中缺少了什么...
Linux cURL [可用]:
curl -v -x http://api.test.sandbox.mobile.de:8080 -u USER:PASS -X POST --data-binary @502.zip https://services.mobile.de/upload-api/upload/502.zip
响应:
POST /upload-api/upload/502.zip HTTP/1.1
User-Agent: curl/7.38.0
Host: services.mobile.de
Accept: */*
Content-Length: 6026
Content-Type: application/x-www-form-urlencoded
Expect: 100-continue
HTTP/1.1 100 Continue } [data not shown]
HTTP/1.1 201 Created
Date: Tue, 06 Dec 2016 12:40:41 GMT
Content-Type: text/html;charset=utf-8
Vary: Accept-Encoding
Transfer-Encoding: chunked
PHP cURL [无法工作]:
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Authorization: Basic '. base64_encode("USER:PASS"),
'Content-Type: text/plain'
));
curl_setopt($ch,CURLOPT_PROXY, 'api.test.sandbox.mobile.de:8080');
curl_setopt($ch,CURLOPT_URL, 'https://services.mobile.de/upload-api/upload/502.zip');
curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch,CURLOPT_POST, 1);
curl_setopt($ch,CURLOPT_POSTFIELDS, [ 'file' => new CURLFile('502.zip') ]);
curl_setopt($ch,CURLOPT_VERBOSE, 1);
$result = curl_exec($ch);
curl_close($ch);
回应:
POST /upload-api/upload/502.zip HTTP/1.1
Host: services.mobile.de
Accept: */*
Content-Length: 6225
Expect: 100-continue
Content-Type: text/plain; boundary=------------------------835f6ea7 5f783449
HTTP/1.1 100 Continue
HTTP/1.1 201 Created
Date: Tue, 06 Dec 2016 13:36:21 GMT
Content-Type: text/html;charset=utf-8
Vary: Accept-Encoding
Transfer-Encoding: chunked
现场文档中写道: "上传文件必须以HTTP-Payload方式发送,并且必须是二进制格式,不支持多部分和编码。" 我还注意到Content-Length不相同...为什么?
非常感谢您的建议!
CURLOPT_POST
设置为1(true)应该为上传设置正确的内容类型。它是否更改为Content-Type: application/x-www-form-urlencoded
? - BA_Webimax