Linux cURL vs PHP cURL - POST请求
问题描述:
我必须使用HTTPS上传ZIP文件,而且这只能通过Linux cURL命令进行。我不明白我在PHP卷曲的要求很想念......Linux cURL vs PHP cURL - POST请求
Linux的卷曲[工作]:
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卷曲[不工作]:
$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有效负载和二进制格式,多部分和编码,不支持。” 我也注意到Content-Length不一样...为什么?
非常感谢您的建议!
答
摆脱线:
'Content-Type: text/plain'
您设置了整个消息的内容类型和未正确格式化POST数据。
我试过没有'Content-Type:text/plain',但我得到了同样的结果。 [Content-Length:6225] – Mirela
将'CURLOPT_POST'设置为1(true)应该为上传设置正确的内容类型。它是否更改为'Content-Type:application/x-www-form-urlencoded'? –
不,它是:Content-Type:multipart/form-data;边界= ------------------------ 679815a4dab25422; HTTP/1.1 100继续; HTTP/1.1 201创建;内容类型:text/html; charset = utf-8 – Mirela