curlfile-uploadphp-curlzendesk-apizendesk-app

Upload app package with curl gives status 500 - Zendesk REST API


I'm trying to make a curl post to upload my app, and I have no idea about what am I doing wrong, but I keep getting a 500 error in the response, and I don't know what to do anymore...

// basic settings for your Zendesk
$userName = 'xxxxx@xxxx.com/token';
$apiKey = 'xkalkjsdXASJKDlasaknfkajsfASASASD';

// upload file info
$fileName = 'v5.0.zip';
$filePath = 'path/to/file/v5.0.zip';

$url = 'https://mysubdomain.zendesk.com/api/v2/apps/uploads.json';
$file = fopen($filePath, "r");
$size = filesize($filePath);



    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_USERPWD, $userName.":".$apiKey);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/binary'));
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION ,1);
    curl_setopt($ch, CURLOPT_HEADER, 0); // DO NOT RETURN HTTP HEADERS
    curl_setopt($ch, CURLOPT_RETURNTRANSFER ,1); // RETURN THE CONTENTS OF THE CALL
    curl_setopt($ch, CURLOPT_BINARYTRANSFER, TRUE);
    curl_setopt($ch, CURLOPT_POSTFIELDS, array($fileName => "@".$filePath));
    curl_setopt($ch, CURLOPT_USERAGENT, "MozillaXYZ/1.0");
    $output = curl_exec($ch);
    $code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    fclose($file);
    curl_close($ch);

I've tried into the command line, and it works well:

curl https://subdomain.zendesk.com/api/v2/apps/uploads.json \
  -F uploaded_data=@/path/to/file/v5.0.zip -X POST \
  -u "user/token:tokenpassword"

The question would be, does anyone have an idea what I could be doing wrong? I'm really desperate!

Thank you all,

Daniel


Solution

  • So, at the end I found it.

    Everything was right, but not the content-type, so I changed this line:

    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/binary'));
    

    to:

    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/json'));
    

    And just because of this, the curl request was giving me a "NULL" answer.

    I hope that this helps someone!