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);
?>
@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