Linux cURL和PHP cURL的区别 - POST请求

4

我需要使用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不相同...为什么?

非常感谢您的建议!

1个回答

0

删除这行:

  'Content-Type: text/plain'

您正在设置整个消息的内容类型,但它未正确格式化POST数据。


我尝试过没有使用“Content-Type: text/plain”,但结果相同。[Content-Length: 6225] - Mirela
CURLOPT_POST设置为1(true)应该为上传设置正确的内容类型。它是否更改为Content-Type: application/x-www-form-urlencoded - BA_Webimax
不,它是:Content-Type: multipart/form-data; boundary=------------------------679815a4dab25422; HTTP/1.1 100 Continue; HTTP/1.1 201 Created; Content-Type: text/html;charset=utf-8 - Mirela

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接