Skip to content

Instantly share code, notes, and snippets.

@skipjac
Created July 14, 2011 00:25
Show Gist options
  • Save skipjac/1081630 to your computer and use it in GitHub Desktop.
Save skipjac/1081630 to your computer and use it in GitHub Desktop.
Upload file to zendesk with PHP
<?php
$url = "http://skipjack.zendesk.com/uploads.xml?filename=dropbox1.html";
$file = fopen("dropbox1.html", "r");
$size = filesize($file);
$header= array('Content-type: application/binary' );
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_USERPWD, "skip@youremail.net:passwrod");
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST ,1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_POSTFIELDS ,$file);
curl_setopt($ch, CURLOPT_POST ,1);
curl_setopt($ch, CURLOPT_INFILE, $file);
curl_setopt($ch, CURLOPT_INFILESIZE, strlen($file));
curl_setopt($ch, CURLOPT_FOLLOWLOCATION ,1);
curl_setopt($ch, CURLOPT_HEADER ,1); // DO NOT RETURN HTTP HEADERS
curl_setopt($ch, CURLOPT_RETURNTRANSFER ,1); // RETURN THE CONTENTS OF THE CALL
$token = curl_exec($ch);
?>
@paqtheman
Copy link

I tried this with their V2 API and the uploaded files didn't contain the correct data - when I changed it a bit to use POSTFIELDS instead of INFILE it managed to work (not sure if this is the only way, or if there are other changes I could have made to the sample above).

What worked for me:

    // basic settings for your Zendesk
    $userName = 'youruser@example.com';
    $apiKey = 'apiKeyFromZD';

    // upload file info
    $fileName = 'myfile.txt';
    $filePath = '/full/path/to/myfile.txt';
    $token = NULL; // set to previously returned token to upload multiple files in 1 comment

    $url = 'https://yourdomain.zendesk.com/api/v2/uploads.json?filename='.urlencode($fileName);
    $url .= (is_null($token)) ? '' : '&token='.$token;
    $file = fopen($filePath, "r");
    $size = filesize($filePath);

    $ch = curl_init($this->zdURL.$url);
    curl_setopt($ch, CURLOPT_USERPWD, $userName."/token:".$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);

    if ($code !== 200 && $code !== 201)
      {
        return 'Status code returned was '.$code.'!';
      }

    $decoded = json_decode($output);
    return $decoded;

@aleksbelic
Copy link

Looks like you're calling "curl_setopt($ch, CURLOPT_POST ,1);" two times: lines 10 and 13.

@willryanuk
Copy link

In 2023, the above code was not really working for me... it was almost working, uploading something, and returning JSON, but the uploaded image was blank. In the end, this is the only combination of CURL options that would work for me:

$fileName = 'test.png';
$filePath = 'test.png';

$url = 'https://test.zendesk.com/api/v2/uploads.json?filename=' . urlencode($fileName);
$file = fopen($filePath, "r");
$mimeContentType = mime_content_type($file);
$size = filesize($filePath);


$ch = curl_init($url);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER ,true);
curl_setopt($ch, CURLOPT_POST ,true);
curl_setopt($ch, CURLOPT_POSTFIELDS, file_get_contents($filePath));
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-type: {$mimeContentType}"));
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_USERPWD, "test@test.com/token:XXXXXXXXXXXXXXXXXXX");
$output = curl_exec($ch);
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
fclose($file);
curl_close($ch);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment