Exemplo n.º 1
0
<?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
}
Exemplo n.º 3
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
}
<?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
}
Exemplo n.º 5
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
}
Exemplo n.º 6
0
 /** 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 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
}