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