4

I have to upload a ZIP file using HTTPS and this is working only via Linux cURL command. I don't understand what i am missing in PHP cURL request...

Linux cURL [working]:

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

Response:

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 [not working]:

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

Response:

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

On site documentation it's written: "The upload file must be sent as an HTTP-Payload and in binary format, Multipart and Encoding are not supported." I also noticed that the Content-Length is not the same... Why?

Thank you in advance for your advice!

Mirela
  • 41
  • 4

1 Answers1

0

Get rid of the line:

  'Content-Type: text/plain'

You are setting the content type for the entire message and it is not formatting the POST data correctly.

BA_Webimax
  • 2,714
  • 1
  • 13
  • 15
  • I tried without 'Content-Type: text/plain', but i get the same result. [Content-Length: 6225] – Mirela Dec 06 '16 at 14:55
  • Setting `CURLOPT_POST` to 1 (true) should set the proper content type for upload. Did it change to `Content-Type: application/x-www-form-urlencoded`? – BA_Webimax Dec 06 '16 at 15:00
  • No, it is: 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 Dec 06 '16 at 15:05