Skip to content

Instantly share code, notes, and snippets.

@mgng
Last active December 12, 2015 03:59
Show Gist options
  • Save mgng/4711359 to your computer and use it in GitHub Desktop.
Save mgng/4711359 to your computer and use it in GitHub Desktop.
<?php
/**
* Transloadit.php
*/
/**
* Transloadit class
*
* usage:
*
* $auth_key = 'your auth key';
* $auth_secret = 'your auth secret';
* $steps = array(
* 'convert_urls' => array(
* 'robot' => '/html/convert',
* 'url' => 'http://twitter.com',
* ),
* 'resize_images' => array(
* 'use' => 'convert_urls',
* 'robot' => '/image/resize',
* 'format' => 'jpg',
* ),
* );
*
* $T = new Transloadit( $auth_key, $auth_secret, $steps );
* if ( ! $T->auth() ) {
* exit('error');
* }
* echo $T->getAssenblyUrl();
*/
class Transloadit
{
const API_URL = 'http://api2.transloadit.com/assemblies';
private $_auth_key = null;
private $_auth_secret = null;
private $_expires = null;
private $_steps = null;
private $_assembly_url = null;
private $_assembly_id = null;
private $_auth_res = null;
private $_auth_obj = null;
public function __construct( $auth_key, $auth_secret, $steps = array(), $expires = '+1 hour' ){
$this->_auth_key = $auth_key;
$this->_auth_secret = $auth_secret;
$this->_steps = $steps;
$this->_expires = $expires;
return true;
}
public function auth() {
$params = json_encode( array(
'auth' => array(
'expires' => gmdate( 'Y/m/d H:i:s+00:00', strtotime( $this->_expires ) ),
'key' => $this->_auth_key,
),
'steps' => $this->_steps,
) );
$signature = hash_hmac( 'sha1', $params, $this->_auth_secret );
$data = array(
'params' => $params,
'signature' => $signature,
);
$this->_auth_res = $this->_post( self::API_URL, $data );
$this->_auth_obj = json_decode( $this->_auth_res );
if ( ! $this->_auth_obj || ! isset( $this->_auth_obj->assembly_url ) ) {
return false;
}
$this->_assembly_url = $this->_auth_obj->assembly_url;
$this->_assembly_id = $this->_auth_obj->assembly_id;
return true;
}
public function getAssenblyUrl() {
return $this->_assembly_url;
}
public function getAssenblyId() {
return $this->_assembly_id;
}
public function getAuthObj() {
return $this->_auth_obj;
}
public function getAuthRes() {
return $this->_auth_res;
}
private function _post ( $url, $data = array() ) {
return file_get_contents( $url, false, stream_context_create(
array(
'http'=>array(
'method' => 'POST',
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'content'=> http_build_query( $data ),
'ignore_errors' => true,
)
)
));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment