Skip to content

Instantly share code, notes, and snippets.

@sbkinney
Forked from skipjac/report-csv-dl-zendesk.php
Created August 2, 2012 18:40
Show Gist options
  • Save sbkinney/3239551 to your computer and use it in GitHub Desktop.
Save sbkinney/3239551 to your computer and use it in GitHub Desktop.
This downloads the scheduled report from Zendesk and saves it to the local server.
<?php
/*********************************************************************************
** This waits for the Zendesk Schedule report call and then downloads the **
** file your Local server for you to act on with other functions or programs **
*********************************************************************************/
$xmlDoc = new DOMDocument();
//Reads the POST from Zendesk
$encodedMessage = file_get_contents("php://input");
//loads the XML
$xmlDoc->loadXML($encodedMessage);
//gets the URL from the Zendesk XML callback
$url = $xmlDoc->getElementsByTagName("url")->item(0)->nodeValue;
//give it a admins user name and password to download it
$username = 'youradmin@your.zendesk.com';
$password = 'apassword';
//Grabs the path name, splits for the file name
$csvName = parse_url($url, PHP_URL_PATH);
$csvNamePath = explode('/', $csvName);
$pathCSVsize = sizeof($csvNamePath)-1;
$csvFileName = $csvNamePath[$pathCSVsize];
transport($url, $username, $password, $csvFileName);
//takes the url and file name downloads to your local server
function transport($url, $username, $password, $csvFileName)
{
$cobj = curl_init();
$csvContent = fopen($csvFileName,'w');
curl_setopt($cobj, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($cobj, CURLOPT_USERPWD, $username .":".$password);
curl_setopt($cobj, CURLOPT_URL, $url);
curl_setopt($cobj, CURLOPT_FOLLOWLOCATION, false);
//curl_setopt($cobj, CURLOPT_HEADER, true);
curl_setopt($cobj, CURLOPT_FILE, $csvContent);
//$last_http_result = curl_exec($cobj);
//$last_error = curl_error($cobj);
//$last_http_code = curl_getinfo($cobj ,CURLINFO_HTTP_CODE);
curl_exec($cobj);
curl_close($cobj);
unzipZendeskCSV($csvFileName);
//print $last_http_result."\n";
//print $last_error."\n";
//print $last_http_code."\n";
}
//Unzips the CSV
function unzipZendeskCSV($reportName)
{
$unzipReport = new ZipArchive;
$unzipReport->open($reportName);
//where to extract the report to
$unzipReport->extractTo('/var/www/reports/');
$unzipReport->close();
}
exit;
?>
@sbkinney
Copy link
Author

sbkinney commented Aug 2, 2012

I removed the code that set the headers to .zip as they appeared to be causing issues with this code not working correctly.

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