Skip to content

Instantly share code, notes, and snippets.

@chrisguitarguy
Created September 1, 2011 20:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrisguitarguy/1187256 to your computer and use it in GitHub Desktop.
Save chrisguitarguy/1187256 to your computer and use it in GitHub Desktop.
Dlvr.it API class for WordPress
<?php
// http://pmg.co
class wpDlvrit
{
protected $key;
function __construct( $key = false )
{
$this->set_key( $key );
}
function set_key( $key )
{
if( $key )
{
$this->key = $key;
}
else
{
$opts = get_option( 'wpdlvrit_options' );
$key = isset( $opts['api_key'] ) ? $opts['api_key'] : new WP_Error();
if( is_wp_error( $key ) )
{
$key->add( 'no_api_key', __( "You haven't set up your Dlvr.it API key yet!" ) );
}
$this->key = $key;
}
}
function get_key()
{
return $this->key;
}
function get_routes( $format = 'json' )
{
$data = array(
'method' => 'POST',
'body' => array( 'key' => $this->get_key() )
);
$url = 'http://api.dlvr.it/1/routes.' . $format;
$response = wp_remote_post( $url, $data );
$response = $this->validate( $response );
return $response;
}
function get_accounts( $format = 'json' )
{
$data = array(
'method' => 'POST',
'body' => array( 'key' => $this->get_key() )
);
$url = 'http://api.dlvr.it/1/accounts.' . $format;
$response = wp_remote_post( $url, $data );
$response = $this->validate( $response );
return $response;
}
function get_shortlink( $long_url, $format = 'json' )
{
$data = array(
'method' => 'POST',
'body' => array(
'key' => $this->get_key(),
'url' => $long_url
)
);
$url = 'http://api.dlvr.it/1/shorten.' . $format;
$response = wp_remote_post( $url, $data );
$response = $this->validate( $response );
return $response;
}
function post_to_account( $account_id, $message, $format = 'json' )
{
$data = array(
'method' => 'POST',
'body' => array(
'key' => $this->get_key(),
'id' => $account_id,
'msg' => $message
)
);
$url = 'http://api.dlvr.it/1/postToAccount.' . $format;
$response = wp_remote_post( $url, $data );
$response = $this->validate( $response );
return $response;
}
function post_to_route( $account_id, $message, $format = 'json' )
{
$data = array(
'method' => 'POST',
'body' => array(
'key' => $this->get_key(),
'id' => $account_id,
'msg' => $message
)
);
$url = 'http://api.dlvr.it/1/postToRoute.' . $format;
$response = wp_remote_post( $url, $data );
$response = $this->validate( $response );
return $response;
}
// Validate our responses ...
protected function validate( $response )
{
if( is_wp_error( $response ) || 200 != $response['response']['code'] ) return false;
return $response;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment