Skip to content

Instantly share code, notes, and snippets.

@kythin
Created March 7, 2014 04:59
Show Gist options
  • Save kythin/9405503 to your computer and use it in GitHub Desktop.
Save kythin/9405503 to your computer and use it in GitHub Desktop.
Blitline SDK for PHP, super basic and enough for you to start running things from their examples http://www.blitline.com/docs/examples
<?php
/**
* Class Bitline
*
* by github.com/kythin
*
* http://www.blitline.com/docs/examples
*
* Very basic starter SDK for PHP for this awesome service! From this code you should be able to do pretty much anything.
*
*/
class Blitline {
public $key;
public $url = "http://api.blitline.com/job";
public $response;
public function __construct($key) {
$this->key = $key;
}
/**
* onejob
* A really basic starter function to run basic jobs through blitline.
* See the examples on http://www.blitline.com/docs/examples for what the call the function names, and the paramaters.
*
* @param $src
* @param $function
* @param $params
* @return bool
*/
public function onejob($src, $function, $params) {
$extension = '.'.array_pop(explode('.',$src));
$id = 'image_'.time();
$packet = array();
$packet['application_id'] = $this->key;
$packet['src'] = $src;
$packet['functions'] = array(
array('name'=>$function,
'params'=>$params,
'save'=>array('image_identifier'=>$id,
'extension' => $extension
)
)
);
if ($extension == ".png") {
$packet['functions'][0]['save']['png_quantize'] = true;
}
//print_r($packet);
if ($this->sendRequest(array('json'=>json_encode($packet)))) {
$result = json_decode($this->response, true);
if (count(@$result['results']['images'])) {
return array_pop($result['results']['images'])['s3_url'];
} else {
//print_r($result);
return false;
}
} else {
echo "Something went wrong with the send.";
}
}
/**
* Send Request
* Generic function to send whatever request has been prepared, and save the response.
* @return bool
*/
private function sendRequest($data) {
$url = $this->url;
//init the curl request
$ch = curl_init();
$qry_str = http_build_query($data);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $qry_str);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, '30');
//send it off & save the result
$content = trim(curl_exec($ch));
curl_close($ch);
if ($content) {
$this->response = $content;
return true;
} else {
return false;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment