Created
May 19, 2017 11:16
-
-
Save hobailey/29c055b3d6ed52ae780dfdc636e4482e to your computer and use it in GitHub Desktop.
Download/export knowledge base articles from desk.com (for migration to Salesforce etc)
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 | |
//Note that this is a quick and (very) dirty solution, but works so thought I'd share it :-) | |
//Usage: Complete $loginEmail, $loginPassword and $deskSubdomain and then run the script - it will access Desk for you and download to your browser a CSV of all the articles. Note that you'll need to download manually the images (attachments) etc. | |
function run_desk_api($endpoint, $data=array(), $method="POST") { | |
$loginEmail = ""; | |
$loginPassword = ""; | |
$deskSubdomain = "";//the bit before desk.com when you're viewing articles etc | |
$ch = curl_init('https://'.$deskSubdomain.'.desk.com/api/v2/'.$endpoint); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | |
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method); | |
curl_setopt($ch, CURLOPT_HTTPHEADER, array( | |
"Content-Type: application/json", | |
"Authorization: Basic ".base64_encode($loginEmail.":".$loginPassword) | |
)); | |
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data)); | |
$response = json_decode(curl_exec($ch), true); | |
unset($ch);unset($apiConfig); | |
return $response; | |
} | |
//var_dump(run_desk_api("articles", array(), "GET"));die(); | |
function getArticles() { | |
return run_desk_api("articles", array("per_page"=>200, "page"=>1), "GET"); | |
} | |
function processArticleField($f) { | |
global $directDownload; | |
$f = str_replace("/customer/portal/attachments/", "https://mangopay.desk.com/customer/portal/attachments/", $f); | |
//$f = "<code>".$f."</code>"; | |
//$f = "<pre><code>".$f."</code></pre>"; | |
if (!$directDownload) { | |
$f = "<script type='application/text'>".$f."</script>"; | |
} | |
//$f = html_entity_decode($f); | |
return $f; | |
} | |
$directDownload = true; | |
$articles = getArticles(); | |
if (!$directDownload) { | |
?> | |
<style type="text/css"> | |
script { | |
display: block; | |
} | |
</style> | |
<?php | |
} | |
$headersDone = false; | |
if (!$directDownload) echo "<table>"; | |
$outputData = array(); | |
foreach($articles["_embedded"]["entries"] as $a) { | |
$fieldsToHide = array("_links"); | |
if (!$headersDone) { | |
$titleRow = array(); | |
if (!$directDownload) echo "<tr>"; | |
foreach($a as $f=>$v) { | |
$shouldShowField = !in_array($f, $fieldsToHide); | |
if ($shouldShowField) { | |
if ($directDownload) { | |
$titleRow[] = $f; | |
}else{ | |
echo "<th>$f</th>"; | |
} | |
} | |
} | |
if (!$directDownload) echo "</tr>"; | |
$outputData[] = $titleRow; | |
$headersDone = true; | |
} | |
if (!$directDownload) echo "<tr>"; | |
$thisRow = array(); | |
foreach($a as $f=>$v) { | |
$shouldShowField = !in_array($f, $fieldsToHide); | |
if ($shouldShowField) { | |
if ($directDownload) { | |
$thisRow[] = processArticleField($v); | |
}else{ | |
echo "<td>".processArticleField($v)."</td>"; | |
} | |
} | |
} | |
if (!$directDownload) echo "</tr>"; | |
$outputData[] = $thisRow; | |
} | |
if ($directDownload) { | |
header("Content-Type: text/csv; charset=UTF-8"); | |
header("Content-Disposition: attachment; filename=desk_export_".date("H.i d-m-Y").".csv"); | |
// Disable caching | |
header("Cache-Control: no-cache, no-store, must-revalidate"); // HTTP 1.1 | |
header("Pragma: no-cache"); // HTTP 1.0 | |
header("Expires: 0"); // Proxies | |
$output = fopen("php://output", "w"); | |
foreach ($outputData as $row) { | |
foreach($row as $i=>$a) { | |
$row[$i] = mb_convert_encoding($row[$i], 'UTF-16LE', 'UTF-8');//this makes the file 2x bigger, but at least resolves the accent issue | |
} | |
fputcsv($output, $row); // here you can change delimiter/enclosure | |
} | |
fclose($output); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment