16/12
2015
Bring Tracking API library
This PHP wrapper is not supported by Bring, but as of Dec 2015 they do not offer a PHP alternative. Based on the developer documentation., (*1)
With Composer:, (*2)
composer require animail/bring-api
Voila!, (*3)
use \Animail\BringApi; // ... $api_uid = 'john.doe@example.com'; $api_key = '1234abc-abcd-1234-5678-abcd1234abcd'; $my_website_url = 'https://example.com/'; $bring = new BringApi($api_uid, $api_key, $my_website_url); // Don't want to authenticate? $bring = new BringApi();
The track()
method wraps the Tracking API and always returns an array, including when the shipment(s) could not be found. Unless there was an API error, in which case we'll throw all kinds of fun \Animail\BringApiException
s., (*4)
// Shipment number $consignmentSet = $bring->track('73325389952521130'); // array('consignmentSet' => array()) // Package number $consignmentSet = $bring->track('CT797294851NO'); // array('consignmentSet' => array()) // Reference (this is usually your internal ordernumber or shipmentID) $consignmentSet = $bring->track('1234'); // array('consignmentSet' => array())
Catch everything in the same place, (*5)
try { $bring = new BringApi('', '', ''); } catch (BringApiException $e) { // All our exception types extend the BringApiException class }
Only catch errors from the API, (*6)
try { $bring = new BringApi('', '', ''); $results = $bring->track('boobar'); } catch (BringApiErrorException $e) { }
Separate handlers for different types, (*7)
try { $bring = new BringApi('', '', ''); $results = $bring->track('boobar'); } catch (BringApiClientException $e) { // Issues with the client. Guzzle for some reason threw an error. } catch (BringApiErrorException $e) { } catch (BringApiException $e) { // This one will never be called unless we add more exception types in the future }
We love open source software and would love to see pull requests. We're trying to be PSR-4 compliant but don't have many other requirements., (*8)