PHP Curl File Upload sends Empty Upload PHP Curl File Upload sends Empty Upload curl curl

PHP Curl File Upload sends Empty Upload


The ssl_verify_result has the value of 20, which means, according to the documentation:

unable to get local issuer certificate

the issuer certificate of a locally looked up certificate could not be found. This normally means the list of trusted certificates is not complete.

You can try without verifying the peer first:

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);

If that works, you will have to specify the path of a recent CA bundle. See also: http://curl.haxx.se/docs/sslcerts.html


You should also check whether the file you're trying to upload is in the expected folder. If you specify CURLOPT_VERBOSE = 1 it should warn you about this as well.


Update

After checking the API documentation, the service doesn't expect a regular file upload (i.e. "multipart/form-data"); rather, a raw upload is required.

This can be accomplished by:

curl_setopt($ch, CURLOPT_POSTFIELDS, file_get_contents('myPicture.jpg'));curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: image/jpeg"));

Update 2Passing anything using CURLOPT_POSTFIELDS will implicitly set the request method to POST and Content-Type to application/x-www-form-urlencoded. When you pass an array, the values may start with @ to indicate a file upload (doing so should also implicitly change the Content-Type to multipart/form-data).

The command line curl allows the @ in a few places:

  1. using --data-binary to specify a file containing raw binary data
  2. using --data or --data-ascii to specify a file containing url-encoded data
  3. using --F or --form

The latter behaves the same as passing an array to CURLOPT_POSTFIELDS and using the @ prefix. The other two behave the same as passing a string.