Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?
// make sure the folder of the script is writeble (0777)
$basecampUrl = '[YOUR BASECAMP URL HERE]'; // e.g. https://stelabouras.basecamphq.com/ (Don't forget the trailing slash!)
$apiKey = '[YOUR API KEY HERE]'; // e.g. one huge string (found in 'My info' in the Authentication tokens section)
function BasecampCall($endPoint, $usePrefix = true) {
global $apiKey, $basecampUrl;
// From: http://prattski.com/2008/10/22/basecamp-api-examples-using-php-and-curl-get/
$session = curl_init();
$username = $apiKey;
$password = 'X';
curl_setopt($session, CURLOPT_URL, ($usePrefix == true ? $basecampUrl : "") . $endPoint);
curl_setopt($session, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($session, CURLOPT_HTTPGET, 1);
curl_setopt($session, CURLOPT_HEADER, false);
curl_setopt($session, CURLOPT_HTTPHEADER, array('Accept: application/xml', 'Content-Type: application/xml'));
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
curl_setopt($session,CURLOPT_USERPWD,$username . ":" . $password);
if(ereg("^(https)",$request)) curl_setopt($session,CURLOPT_SSL_VERIFYPEER,false);
$response = curl_exec($session);
curl_close($session);
return ($usePrefix ? simplexml_load_string($response) : $response);
}
$projects = BasecampCall('projects.xml');
// For each project take name and id
foreach($projects->project as $proj) {
$pr = array(
"id" => (string)$proj->id,
"name" => (string)$proj->name
);
$n = 0;
$i = 0;
// Retrieve the attachments
echo "\nSaving attachments for project: " . $pr['name'] . "...\n";
@mkdir($pr['name']);
$filesArray = array();
do {
$attachments = BasecampCall("projects/" . $proj->id . "/attachments.xml?n=" . $n);
if(count($attachments->attachment) > 0) {
foreach($attachments->attachment as $attachment) {
$file = pathinfo($attachment->name);
@file_put_contents($pr['name'] . "/" . $file['filename'] . (in_array($file['filename'], $filesArray) ? "-" . rand() : "") . "." . $file['extension'], BasecampCall($attachment->{'download-url'}, false));
$filesArray[] = $file['filename'];
echo "Saving file " . $attachment->name . "...\n";
}
}
$n += 100;
} while(count($attachments->attachment) == 100);
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment