PHP code example of firewizard / fancourier-api
1. Go to this page and download the library: Download firewizard/fancourier-api library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
firewizard / fancourier-api example snippets
$clientId = 'your_client_id';
$username = 'your_username';
$password = 'your_password';
$fan = new Fancourier\Fancourier($clientId, $username, $password);
$fan = Fancourier\Fancourier::testInstance();
class FancourierAuthCache implements AuthTokenCacheContract
{
const CACHE_KEY = 'fancourier_auth_token';
const CACHE_LIFETIME = 43200; //12 hrs
public function get()
{
return Cache::get(static::CACHE_KEY);
}
public function set($value)
{
Cache::put(static::CACHE_KEY, $value, static::CACHE_LIFETIME);
}
}
$api = new Fancourier(...);
$api->useAuthTokenCache(new FancourierAuthCache());
$request = new Fancourier\Request\GetRates();
$request
->setParcels(1)
->setWeight(2)
->setRegion('Arad')
->setCity('Aciuta')
->setDeclaredValue(125);
$response = $fan->getRates($request);
if ($response->isOk()) {
var_dump($response->getBody());
} else {
var_dump($response->getErrorMessage());
}
$request = new Fancourier\Request\CreateAwb();
$request
->setParcels(1)
->setWeight(2)
->setReimbursement(125)
->setDeclaredValue(125)
->setNotes('testing notes')
->setContents('SKU-1, SKU-2')
->setRecipient("John Ivy")
->setPhone('0723000000')
->setRegion('Arad')
->setCity('Aciuta')
->setStreet('Str Lunga nr 1');
$response = $fan->createAwb($request);
if ($response->isOk()) {
var_dump($response->getBody());
} else {
var_dump($response->getErrorMessage());
}
$batchRequest = new Fancourier\Request\CreateAwbBulk();
$request = new Fancourier\Request\CreateAwb();
$request
->setParcels(1)
->setWeight(2)
->setReimbursement(125)
->setDeclaredValue(125)
->setNotes('testing notes')
->setContents('SKU-1, SKU-2')
->setRecipient("John Ivy")
->setPhone('0723000000')
->setRegion('Arad')
->setCity('Aciuta')
->setStreet('Str Lunga nr 1')
;
$batchRequest->append($request);
$request
->setParcels(1)
->setWeight(1.5)
->setReimbursement(50)
->setDeclaredValue(50)
->setContents('SKU-7')
->setRecipient("Tester Testerson")
->setPhone('0722111000')
->setRegion('Sibiu')
->setCity('Sibiu')
->setStreet('Calea Bucuresti nr 1')
;
$batchRequest->append($request);
$response = $fan->createAwbBulk($batchRequest);
if (!$response->isOk()) {
//general error
die($response->getErrorMessage());
}
foreach ($response->getBody() as $awb) {
echo $awb . "\n";
}
$request = new Fancourier\Request\TrackAwb();
$request->setAwb('2150900120086');
$response = $fan->trackAwb($request);
if ($response->isOk()) {
print_r($response->getBody());
} else {
print_r($response->getErrorMessage());
}
$request = new Fancourier\Request\PrintAwb();
$request->setAwb('2150900120086');
$response = $fan->printAwb($request);
if ($response->isOk()) {
echo $response->getBody();
} else {
var_dump($response->getErrorMessage());
}
$request = new Fancourier\Request\PrintAwbHtml();
$request->setAwb('2150900120086');
$response = $fan->printAwbHtml($request);
if ($response->isOk()) {
echo $response->getBody();
} else {
var_dump($response->getErrorMessage());
}
$request = new Fancourier\Request\DeleteAwb();
$request->setAwb('2150900120086');
$response = $fan->deleteAwb($request);
if ($response->isOk()) {
var_dump($response->getBody());
} else {
var_dump($response->getErrorMessage());
}
$request = new Fancourier\Request\TrackAwbBulk();
$request->setAwbs(['2162900120047']);
$response = $fan->trackAwbBulk($request);
if ($response->isOk()) {
print_r($response->getBody());
} else {
var_dump("ERROR: " . $response->getErrorMessage());
}
$response = $fan->getCities();
if ($response->isOk()) {
print_r($response->getBody());
} else {
var_dump("ERROR: " . $response->getErrorMessage());
}
$response = $fan->getCounties();
if ($response->isOk()) {
print_r($response->getBody());
} else {
var_dump("ERROR: " . $response->getErrorMessage());
}
$response = $fan->getServices();
if ($response->isOk()) {
print_r($response->getBody());
} else {
var_dump("ERROR: " . $response->getErrorMessage());
}