<?php

require 'credentials.php';
use Postmen\Postmen;
// TODO put your shipper account ID here
$shipper = NULL;
if (!isset($shipper)) {
    echo "\$shipper is not set, modify file manifests_create.php\n";
}
$payload = array('shipper_account' => array('id' => $shipper), 'async' => false);
try {
    $api = new Postmen($key, $region);
    $result = $api->create('manifests', $payload);
    echo "RESULT:\n";
    print_r($result);
} catch (exception $e) {
    echo "ERROR:\n";
    echo $e->getCode() . "\n";
    // error code
    echo $e->getMessage() . "\n";
    // error message
    print_r($e->getDetails());
    // error details
}
<?php

require 'credentials.php';
use Postmen\Postmen;
try {
    $api = new Postmen($key, $region);
    // get a raw json string
    $output_json = $api->get('labels', NULL, NULL, array('raw' => true));
    // get a std object
    $output_object = $api->get('labels');
    // to get an array it requires to initiate
    // handler object with that option
    $array_api = new Postmen($key, $region, array('array' => true));
    $output_array = $array_api->get('labels');
    echo "RESULT:\n";
    echo "raw json output:\n";
    echo $output_json . "\n\n";
    echo "std object output:\n";
    print_r($output_object);
    echo "\narray object output\n";
    print_r($output_array);
} catch (exception $e) {
    echo "ERROR:\n";
    echo $e->getCode() . "\n";
    // error code
    echo $e->getMessage() . "\n";
    // error message
    print_r($e->getDetails());
    // error details
}
<?php

require 'credentials.php';
use Postmen\Postmen;
// TODO put ID of a particular rate
$rate = NULL;
if (!isset($rate)) {
    echo "\$rate is not set, modify file rates_retrieve.php\n";
}
try {
    $api = new Postmen($key, $region);
    // retrieve all the rates
    $result_all = $api->get('rates');
    // retrieve a particular rate
    $result_particular = $api->get('rates', $rate);
    echo "RESULT:\n";
    print_r($result_all);
    print_r($result_particular);
} catch (exception $e) {
    echo "ERROR:\n";
    echo $e->getCode() . "\n";
    // error code
    echo $e->getMessage() . "\n";
    // error message
    print_r($e->getDetails());
    // error details
}
<?php

require 'credentials.php';
use Postmen\Postmen;
// TODO put the ID of the label you wish to cancel
$label = NULL;
if (!isset($label)) {
    echo "\$label is not set, modify file cancel_label.php\n";
}
$payload = array('label' => array('id' => $label));
try {
    $api = new Postmen($key, $region);
    $result = $api->create('cancel-labels', $payload);
    echo "RESULT:\n";
    print_r($result);
} catch (exception $e) {
    echo "ERROR:\n";
    echo $e->getCode() . "\n";
    // error code
    echo $e->getMessage() . "\n";
    // error message
    print_r($e->getDetails());
    // error details
}
Beispiel #5
0
<?php

require 'credentials.php';
use Postmen\Postmen;
$proxy = array("host" => "proxyserver.com", "port" => 9999, "username" => "user", "password" => "pass");
try {
    // putting proxy in the constructor sets
    // it by default for all calls
    $api = new Postmen($key, $region, array('proxy' => $proxy));
    // putting the proxy object in a call
    // will make it be used only once, this
    // can also be used to disable it for purpose
    // of a single call
    $result = $api->get('labels', array('proxy' => $proxy));
} catch (exception $e) {
    echo "ERROR:\n";
    echo $e->getCode() . "\n";
    // error code
    echo $e->getMessage() . "\n";
    // error message
    print_r($e->getDetails());
    // error details
}
Beispiel #6
0
// exception which will inform you what
// is wrong with your payload
echo "using try ... catch\n";
try {
    $api = new Postmen('THIS IS NOT A VALID API KEY', $region);
    $result = $api->get('labels');
} catch (exception $e) {
    echo "ERROR:\n";
    echo $e->getCode() . "\n";
    // error code
    echo $e->getMessage() . "\n";
    // error message
    print_r($e->getDetails());
    // error details
}
// we also can enable the safe mode,
// this way try{}catch(){} is no
// longer required
echo "using safe mode\n";
$api = new Postmen('THIS IS NOT A VALID API KEY', $region);
$result = $api->get('labels', NULL, array('safe' => true));
if (!$result) {
    $e = $api->getError();
    echo "ERROR:\n";
    echo $e->getCode() . "\n";
    // error code
    echo $e->getMessage() . "\n";
    // error message
    print_r($e->getDetails());
    // error details
}
<?php

require 'credentials.php';
use Postmen\Postmen;
// TODO put ID of a particular manifest
$manifest = NULL;
if (!isset($manifest)) {
    echo "\$manifest is not set, modify file manifests_retrieve.php\n";
}
try {
    $api = new Postmen($key, $region);
    // retrieve all the manifests
    $result_all = $api->get('manifests');
    // retrieve a particular manifest
    $result_particular = $api->get('manifests', $manifest);
    echo "RESULT:\n";
    print_r($result_all);
    print_r($result_particular);
} catch (exception $e) {
    echo "ERROR:\n";
    echo $e->getCode() . "\n";
    // error code
    echo $e->getMessage() . "\n";
    // error message
    print_r($e->getDetails());
    // error details
}
 /** test array optional parameter
  */
 public function testReturnAsArray()
 {
     $handler = new Postmen('', 'region', array('array' => true));
     $curl_response = $this->headers . '{"meta":{"code":200,"message":"OK"},"data":{"key1":"value1", "key2":"value2"}}';
     $mock_curl = new PHPUnit_Extensions_MockFunction('curl_exec', $handler);
     $mock_curl->expects($this->at(0))->will($this->returnValue($curl_response));
     $mock_curl_length = new PHPUnit_Extensions_MockFunction('curl_getinfo', $handler);
     $mock_curl_length->expects($this->atLeastOnce())->will($this->returnValue($this->headers_length));
     $result = $handler->get('labels', '');
     $this->assertEquals(isset($result['key1']), true);
     $this->assertEquals(isset($result['key2']), true);
     $this->assertEquals($result['key1'], 'value1');
     $this->assertEquals($result['key2'], 'value2');
 }
<?php

require 'credentials.php';
use Postmen\Postmen;
// TODO put ID of your shipper account
$shipper = NULL;
if (!isset($shipper)) {
    echo "\$shipper is not set, modify file rates_calculate.php\n";
}
$item = array('description' => 'PS4', 'origin_country' => 'JPN', 'quantity' => 2, 'price' => array('amount' => 50, 'currency' => 'JPY'), 'weight' => array('value' => 0.6, 'unit' => 'kg'), 'sku' => 'PS4-2015');
$sender = array('contact_name' => 'Yin Ting Wong', 'street1' => 'Flat A, 30/F, Block 17 Laguna Verde', 'city' => 'Hung Hom', 'state' => 'Kowloon', 'country' => 'HKG', 'phone' => '96679797', 'email' => '*****@*****.**', 'type' => 'residential');
$receiver = array('contact_name' => 'Mike Carunchia', 'street1' => '9504 W Smith ST', 'city' => 'Yorktown', 'state' => 'Indiana', 'postal_code' => '47396', 'country' => 'USA', 'phone' => '7657168649', 'email' => '*****@*****.**', 'type' => 'residential');
$payload = array('async' => false, 'shipper_accounts' => array(0 => array('id' => $shipper)), 'shipment' => array('parcels' => array(0 => array('box_type' => 'custom', 'weight' => array('value' => 0.5, 'unit' => 'kg'), 'dimension' => array('width' => 20, 'height' => 10, 'depth' => 10, 'unit' => 'cm'), 'items' => array(0 => $item))), 'ship_from' => $sender, 'ship_to' => $receiver), 'is_document' => false);
try {
    $api = new Postmen($key, $region);
    $result = $api->create('rates', $payload);
    echo "RESULT:\n";
    print_r($result);
} catch (exception $e) {
    echo "ERROR:\n";
    echo $e->getCode() . "\n";
    // error code
    echo $e->getMessage() . "\n";
    // error message
    print_r($e->getDetails());
    // error details
}
<?php

require 'credentials.php';
use Postmen\Postmen;
// TODO put ID of a particular label
$label = NULL;
if (!isset($label)) {
    echo "\$label is not set, modify file labels_retrieve.php\n";
}
try {
    $api = new Postmen($key, $region);
    // get all the labels
    $result_all = $api->get('labels');
    // get a particular label
    $result_particular = $api->get('labels', $label);
    echo "RESULT:\n";
    print_r($result_all);
    print_r($result_particular);
} catch (exception $e) {
    echo "ERROR:\n";
    echo $e->getCode() . "\n";
    // error code
    echo $e->getMessage() . "\n";
    // error message
    print_r($e->getDetails());
    // error details
}
<?php

require 'credentials.php';
use Postmen\Postmen;
// TODO put ID of your shipper account
$shipper = NULL;
if (!isset($shipper)) {
    echo "\$shipper is not set, modify file labels_create.php\n";
}
$parcel = array('description' => 'info about the parcel', 'box_type' => 'custom', 'weight' => array('value' => 1.5, 'unit' => 'kg'), 'dimension' => array('width' => 20, 'height' => 30, 'depth' => 40, 'unit' => 'cm'), 'items' => array(0 => array('description' => 'Food Bar', 'origin_country' => 'USA', 'quantity' => 2, 'price' => array('amount' => 50, 'currency' => 'USD'), 'weight' => array('value' => 0.6, 'unit' => 'kg'))));
$sender = array('contact_name' => 'your name', 'company_name' => 'name of your company', 'street1' => 'your address', 'street2' => null, 'street3' => null, 'city' => 'your city', 'state' => 'your state', 'postal_code' => 'your postal code', 'country' => 'HKG', 'phone' => '1-403-504-5496', 'fax' => '1-403-504-5497', 'tax_id' => null, 'email' => '*****@*****.**', 'type' => 'business');
$receiver = array('contact_name' => 'Rick McLeod (RM Consulting)', 'street1' => '71 Terrace Crescent NE', 'street2' => 'This is the second streeet', 'city' => 'Medicine Hat', 'state' => 'Alberta', 'postal_code' => 'T1C1Z9', 'country' => 'CAN', 'phone' => '1-403-504-5496', 'email' => '*****@*****.**', 'type' => 'residential');
$payload = array('is_document' => false, 'return_shipment' => false, 'paper_size' => 'default', 'service_type' => 'hong-kong-post_air_parcel', 'customs' => array('billing' => array('paid_by' => 'shipper', 'method' => array('account_number' => '950000002', 'type' => 'account')), 'purpose' => 'gift'), 'shipper_account' => array('id' => $shipper), 'shipment' => array('parcels' => array(0 => $parcel), 'ship_from' => $sender, 'ship_to' => $receiver));
try {
    $api = new Postmen($key, $region);
    $result = $api->create('labels', $payload);
    echo "RESULT:\n";
    print_r($result);
} catch (exception $e) {
    echo "ERROR:\n";
    echo $e->getCode() . "\n";
    // error code
    echo $e->getMessage() . "\n";
    // error message
    print_r($e->getDetails());
    // error details
}
Beispiel #12
0
<?php

require 'credentials.php';
use Postmen\Postmen;
// TODO put the ID of the label you wish to cancel
$label = NULL;
if (!isset($label)) {
    echo "\$label is not set, modify file cancel_label.php\n";
}
try {
    $api = new Postmen($key, $region);
    $result = $api->cancelLabel($label);
    echo "RESULT:\n";
    print_r($result);
} catch (exception $e) {
    echo "ERROR:\n";
    echo $e->getCode() . "\n";
    // error code
    echo $e->getMessage() . "\n";
    // error message
    print_r($e->getDetails());
    // error details
}