postmen / sdk-php
PHP for Postmen API. This extension helps developers to integrate with Postmen easily.
Requires (Dev)
- phpunit/phpunit: 3.7.*
- tcz/phpunit-mockfunction: v1.0.0
This package is not auto-updated.
Last update: 2024-11-13 19:01:01 UTC
README
PHP SDK for Postmen API. For problems and suggestions please open GitHub issue
Table of Contents
- Installation
- Quick Start
- class Postmen
- Postmen($api_key, $region, $config = array())
- create($resource, $payload, $config = array())
- get($resource, $id = NULL, $query = array(), $config = array())
- getError()
- callGET($path, $query = array(), $options = array())
- callPOST($path, $body = array(), $options = array())
- callPUT($path, $body = array(), $options = array())
- callDELETE($path, $body = array(), $options = array())
- Error Handling
- Examples
- Testing
- License
- Contributors
Installation
Requirements
PHP version >= 5.3
is required. For SDK development PHP 5.6
is required (to run automated tests).
Tested on PHP 5.3, 5.4, 5.5, 5.6.
Manual installation
- Download the source code.
- Reference API class.
require('.../path/to/repository/src/Postmen/Postmen.php');
Using Composer
- If you don't have Composer, download and install
- You have 2 options to download the Postmen PHP SDK
Run the following command to require Postmen PHP SDK
composer require postmen/sdk-php
OR download the sorce code and run
composer install
- Autoload the
postmen-php-sdk
package.
$loader = require __DIR__ . '/vendor/autoload.php';
Quick Start
In order to get API key and choose a region refer to the documentation.
use Postmen\Postmen; $api_key = 'YOUR_API_KEY'; $region = 'sandbox'; // create Postmen API handler object $api = new Postmen($api_key, $region); try { // as an example we request all the labels $result = $api->get('labels'); echo "RESULT:\n"; print_r($result); } catch (exception $e) { // if error occurs we can access all // the details in following way echo "ERROR:\n"; echo $e->getCode() . "\n"; // error code echo $e->getMessage() . "\n"; // error message print_r($e->getDetails()); // details }
class Postmen
Postmen($api_key, $region, $config = array())
Initiate Postmen SDK object. In order to get API key and choose a region refer to the documentation.
create($resource, $payload, $config = array())
Creates API $resource
object, returns new object payload as Array
.
API Docs:
Examples:
get($resource, $id = NULL, $query = array(), $config = array())
Gets API $resource
objects (list or a single objects).
API Docs:
- GET /rates
- GET /rates/:id
- GET /labels
- GET /labels/:id
- GET /manifests
- GET /manifests/:id
- GET /cancel-labels
- GET /cancel-labels/:id
Examples:
getError()
Returns SDK error, PostmenException type if $conifg['safe'] = TRUE;
was set.
Check Error Handling for details.
callGET($path, $query = array(), $options = array())
Performs HTTP GET request, returns an Array
object holding API response.
callPOST($path, $body = array(), $options = array())
callPUT($path, $body = array(), $options = array())
callDELETE($path, $body = array(), $options = array())
Performs HTTP POST/PUT/DELETE request, returns an Array
object holding API response.
Error Handling
Particular error details are listed in the documentation.
All SDK methods may throw an exception described below.
class PostmenException
In case of $conifg['safe'] = TRUE;
SDK would not throw exceptions, getError() must be used instead.
Example: error.php
Automatic retry on retryable error
If API error is retryable, SDK will wait for delay and retry. Delay starts from 1 second. After each try, delay time is doubled. Maximum number of attempts is 5.
To disable this option set $conifg['retry'] = FALSE;
Examples
Full list
All examples avalible listed in the table below.
How to run
Download the source code, go to examples
directory.
Put your API key and region to credentials.php
Check the file you want to run before run. Some require you to set additional variables.
Navigation table
For each API method SDK provides PHP wrapper. Use the table below to find SDK method and example that match your need.
Testing
If you contribute to SDK, run automated test before you make pull request.
phpunit --bootstrap tests/bootstrap.php tests/Postmen.php
License
Released under the MIT license. See the LICENSE file for details.
Contributors
- Sunny Chow - view contributions
- Marek Narozniak - view contributions