Created
July 14, 2011 00:25
-
-
Save skipjac/1081630 to your computer and use it in GitHub Desktop.
Upload file to zendesk with PHP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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); | |
?> |
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;
Looks like you're calling "curl_setopt($ch, CURLOPT_POST ,1);" two times: lines 10 and 13.
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
This is the response you get
HTTP/1.1 200 OK
53777038Server: nginx/0.8.53
Date: Thu, 14 Jul 2011 00:23:23 GMT
Content-Type: application/xml; charset=utf-8
Connection: keep-alive
Status: 200 OK
ETag: "517caa53231c26c5c89693c3c1430db4"
X-UA-Compatible: IE=EmulateIE7
X-Runtime: 1197
Content-Length: 116
X-API-Deprecated: Please use /api/v1/uploads.xml
Set-Cookie: _zendesk_session=BAh7DCIcd2FyZGVuLnVzZXIuZGVmYXVsdC5rZXlpAwH8VzoMYWNjb3VudGkCMjs6DWF1dGhfdmlhIhJCYXNpY1N0cmF0ZWd5OhVhdXRoZW50aWNhdGVkX2F0bCsH%2BjYeTiITd2FyZGVuLm1lc3NhZ2V7ADoPc2Vzc2lvbl9pZCIlODZjN2JmOTdhNGM5NTQ0Y2IyYmVmZDNiNGM5OTA0NzE6B2lkIhVlbDRsbzltNHotYXphZmNy--bbe95edc34135ef88c785762e5b89ee9941a84d2; path=/; HttpOnly
Set-Cookie: oid_user=; path=/; expires=Thu, 01-Jan-1970 00:00:00 GMT
Cache-Control: private, max-age=0, must-revalidate
X-Zendesk-API-Version: v1
Use the token in the next step to add to the ticket