Created
February 27, 2013 15:30
-
-
Save duncanjbrown/5048763 to your computer and use it in GitHub Desktop.
WordPress implementation of the AfterShip API (http://aftership.com).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// could do with some DRYing up | |
/** | |
* Usage: | |
* | |
* $aftership = AfterShip::get_instance(); | |
* | |
* // get the tracking info (returns WP_Error with message on failure, JSON on success) | |
* $aftership->get_tracking_info( "tracking code", "aftership courier slug"); | |
* | |
* // submit a new tracking code (returns WP_Error with message on failure, true on success) | |
* $aftership->submit_tracking_code( "tracking code", "aftership courier slug"); | |
* | |
* // get the list of couriers in JSON | |
* $aftership->get_couriers(); | |
*/ | |
define( 'AFTERSHIP_KEY', YOUR KEY HERE ); | |
/** | |
* Interface for the AfterShip API | |
*/ | |
class AfterShip { | |
static private $_instance = null; | |
public static function & get_instance() { | |
if ( is_null( self::$_instance ) ) { | |
self::$_instance = new self(); | |
} | |
return self::$_instance; | |
} | |
private $endpoint = 'https://api.aftership.com'; | |
private function __construct() {} | |
/** | |
* Get the JSON list of couriers | |
* @return string | |
*/ | |
public function get_couriers() { | |
return $this->get( '/v2/couriers.json', array( 'api_key' => AFTERSHIP_KEY ) ); | |
} | |
/** | |
* Submit a code to AfterShip | |
* @param string $code the code to send | |
* @param string $courier the carrier | |
* @return true|WP_Error | |
*/ | |
public function submit_tracking_code( $code, $courier ) { | |
$response = $this->post( '/v2/trackings.json', array( | |
'api_key' => AFTERSHIP_KEY, | |
'tracking_number' => $code, | |
'courier' => $courier ) ); | |
if( $response->success == true ) | |
return true; | |
else | |
return new WP_Error( 'aftership_error', $response->message ); | |
} | |
/** | |
* Get some tracking info from AS | |
* @param string $code the code | |
* @param string $courier the carrier | |
* @return string|WP_Error | |
*/ | |
public function get_tracking_info( $code, $courier ) { | |
$response = $this->get( '/v2/trackings.json', array( | |
'api_key' => AFTERSHIP_KEY, | |
'tracking_number' => $code, | |
'courier' => $courier ) ); | |
if( $response->success == true ) | |
return true; | |
else | |
return new WP_Error( 'aftership_error', $response->message ); | |
} | |
/** | |
* Make a GET request to the API | |
* @param string $path the path on the api | |
* @param array $args the args to encode | |
* @throws Exception on API failure | |
* @return string the response body | |
*/ | |
private function get( $path, $args ) { | |
$query = http_build_query( $args ); | |
$response = wp_remote_get( $this->endpoint . $path . '?' . $query ); | |
if( !$response['body'] ) { | |
throw new Exception( 'Aftership API failure' ); | |
} else { | |
return json_decode( $response['body'] ); | |
} | |
} | |
/** | |
* Make a POST request to the API | |
* @param string $path the path on the api | |
* @param array $args the args to encode | |
* @throws Exception on API failure | |
* @return string the response body | |
*/ | |
private function post( $path, $args ) { | |
$options = array( 'body' => $args ); | |
$response = wp_remote_post( $this->endpoint . $path, $options ); | |
if( !$response['body'] ) { | |
throw new Exception( 'Aftership API failure' ); | |
} else { | |
return json_decode( $response['body'] ); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment