Skip to content

Instantly share code, notes, and snippets.

@thinkstylestudio
Created September 8, 2012 23:26
Show Gist options
  • Save thinkstylestudio/3680991 to your computer and use it in GitHub Desktop.
Save thinkstylestudio/3680991 to your computer and use it in GitHub Desktop.
<?php
// Set these dependant on your BB credentials
$username = 'username';
$password = 'password';
// Grab the data from BB's POST service and decode
$json = stripslashes($_POST['payload']);
$data = json_decode($json);
// Set some parameters to fetch the correct files
$uri = $data->repository->absolute_url;
$node = $data->commits[0]->node;
$files = $data->commits[0]->files;
// Foreach through the files and curl them over
foreach ($files as $file) {
if ($file->type == "removed") {
unlink($file->file);
} else {
$url = "https://api.bitbucket.org/1.0/repositories".$uri."raw/".$node."/".$file->file;
$path = $file->file;
$dirname = dirname($path);
if (!is_dir($dirname)){
mkdir($dirname, 0775, true);
}
$fp = fopen($path, 'w');
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_FILE, $fp);
$data = curl_exec($ch);
curl_close($ch);
fclose($fp);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment