OTA upload via PHP curl

Hello,

I have a strange error when trying to upload OTA from a app via a PHP script.
Below is the script:

<?
$userAgent = $_SERVER['HTTP_USER_AGENT'];
$target_url = 'https://mdash.net/api/v2/devices/'.'device_name'.'/ota?access_token='.'token';
$file_name_with_full_path = realpath('20200202-162927.zip');
$post = array('file'=>'@'.$file_name_with_full_path);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$target_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
$result=curl_exec ($ch);
http_response_code(200);
if (curl_errno($ch)) {
$error_msg = curl_error($ch);
}
echo $result;
curl_close ($ch);
?>

Above script works OK if php from hosting service is set to 5.4, but if I set php to 5.6 i get folowing response > {"message":"Internal Server Error"}

Anny ideas?

Please debug it - see https://stackoverflow.com/questions/3757071/php-debugging-curl

Include the debug output. We need to see what PHP sends to mDash.

Debug with PHP 5.6

*   Trying 148.251.54.236...
* TCP_NODELAY set
* Connected to mdash.net (148.251.54.236) port 443 (#0)
* ALPN, offering h2
* ALPN, offering http/1.1
* Cipher selection: ALL:!EXPORT:!EXPORT40:!EXPORT56:!aNULL:!LOW:!RC4:@STRENGTH
* successfully set certificate verify locations:
*   CAfile: /etc/pki/tls/certs/ca-bundle.crt
  CApath: none
* SSL connection using TLSv1.2 / ECDHE-RSA-AES256-GCM-SHA384
* ALPN, server accepted to use http/1.1
* Server certificate:
*  subject: CN=mdash.net
*  start date: Jan 24 10:30:25 2020 GMT
*  expire date: Apr 23 10:30:25 2020 GMT
*  subjectAltName: host "mdash.net" matched cert's "mdash.net"
*  issuer: C=US; O=Let's Encrypt; CN=Let's Encrypt Authority X3
*  SSL certificate verify ok.
> POST /api/v2/devices/device_name/ota?access_token=token HTTP/1.1
Host: mdash.net
Accept: */*
Content-Length: 192
Content-Type: multipart/form-data; boundary=------------------------83f40e9da22bcd68

< HTTP/1.1 500 Internal Server Error
< Server: nginx/1.12.2
< Date: Tue, 03 Mar 2020 13:43:25 GMT
< Content-Type: application/json; charset=UTF-8
< Content-Length: 35
< Connection: keep-alive
< Cache-Control: no-cache, private, max-age=0
< Expires: Thu, 01 Jan 1970 00:00:00 UTC
< Pragma: no-cache
* HTTP error before end of send, stop sending
< 
* Closing connection 0

Debug with PHP 5.4

*   Trying 148.251.54.236...
* TCP_NODELAY set
* Connected to mdash.net (148.251.54.236) port 443 (#0)
* ALPN, offering h2
* ALPN, offering http/1.1
* Cipher selection: ALL:!EXPORT:!EXPORT40:!EXPORT56:!aNULL:!LOW:!RC4:@STRENGTH
* successfully set certificate verify locations:
*   CAfile: /etc/pki/tls/certs/ca-bundle.crt
  CApath: none
* SSL connection using TLSv1.2 / ECDHE-RSA-AES256-GCM-SHA384
* ALPN, server accepted to use http/1.1
* Server certificate:
*  subject: CN=mdash.net
*  start date: Jan 24 10:30:25 2020 GMT
*  expire date: Apr 23 10:30:25 2020 GMT
*  subjectAltName: host "mdash.net" matched cert's "mdash.net"
*  issuer: C=US; O=Let's Encrypt; CN=Let's Encrypt Authority X3
*  SSL certificate verify ok.
> POST /api/v2/devices/device_name/ota?access_token=token HTTP/1.1
Host: mdash.net
Accept: */*
Content-Length: 961869
Content-Type: multipart/form-data; boundary=------------------------131318ab7ea950c6
Expect: 100-continue

< HTTP/1.1 100 Continue
< HTTP/1.1 200 OK
< Server: nginx/1.12.2
< Date: Tue, 03 Mar 2020 13:49:26 GMT
< Content-Type: application/json; charset=UTF-8
< Content-Length: 4
< Connection: keep-alive
< Cache-Control: no-cache, private, max-age=0
< Expires: Thu, 01 Jan 1970 00:00:00 UTC
< Pragma: no-cache
< 
* Connection #0 to host mdash.net left intact

I do not see what’s inside form data, but I can see a big difference in content length - 192 bytes vs 961869 bytes. The newer version , apparently, does not include the file. Maybe it sends only file path, but not the file contents.

Yes, after ponting me to debug the curl output I observe the same thing and also managed to fix the issue.
More details about the problem are detailed here cURL file uploads not working anymore after upgrade from PHP 5.5 to 5.6

New working code is here:

<?
$userAgent = $_SERVER['HTTP_USER_AGENT'];
$target_url = 'https://mdash.net/api/v2/devices/'.'device name'.'/ota?access_token='.'token';
$file_name_with_full_path = realpath('20200202-162927.zip');
$post['file'] = curl_file_create($file_name_with_full_path, 'application/zip', '20200202-162927.zip');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$target_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
$result=curl_exec ($ch);
http_response_code(200);
if (curl_errno($ch)) {
$error_msg = curl_error($ch);
}
echo $result;
curl_close ($ch);
?>
1 Like

Cool, thanks for the background