Skip to content

Basic usage

Zsolt Kaveczki edited this page Sep 15, 2020 · 2 revisions

You need an account to use the API client. Account object must implement the \Webapix\GLS\Contracts\Account interface.

You can use our \Webapix\GLS\Models\Account model to initiate an account:

use \Webapix\GLS\Models\Account;

$account = new Account('api_url', 'client_number', 'username', 'password');

Or you can create your own by implement the interface.

You can find the correct API URL for your location in the official MyGLS docs.

Create a request (Available requests):

$request = new \Webapix\GLS\Requests\GetParcelStatuses('123456789');

Send the request to GLS:

use GuzzleHttp\Client as HttpClient;
use Webapix\GLS\Client;

$api = new Client(new HttpClient);
$api->on(<class that extends \Webapix\GLS\Contracts\Account>)
    ->request(<class that extends \Webapix\GLS\Contracts\Request>);

If there is a client or server error, the request throw an \Webapix\GLS\Exception\RequestException. You can access the original response object (Psr\Http\Message\ResponseInterface) from the exception.

The GLS API return an Webapix\GLS\Contracts\Response object.

$api = (new \Webapix\GLS\Client(new GuzzleHttp\Client));
$response = $api->on(new DefaultAccount)->request($request);

// Check if request was success
if ($response->successfull()) {
    //
}

Each request has unique response data, please check our examples and the Official GLS Docs for more information.

You can get the original response data:

$response->data();

Each response class from API methods includes list of error objects describing potential problems.

// Get errors list, it will return with an \Webapix\GLS\ErrorCollection
$response->errors();

Errors wrapped in \Webapix\GLS\ErrorCollection.

// Get the first error message
$response->errors()->firstErrorMessage();

// Get the first error code
$response->errors()->firstErrorCode();

// Get the first error object
$response->errors()->first();

// Get all errors
$response->errors()->all();

// Get number of errors
$response->errors()->count();

// Check if it has any errors
$response->errors()->hasAny();

// Check if it has error with error code
$response->errors()->has(20);

// Get an error with error code
$response->errors()->get(20);

ErrorCollection contains \Webapix\GLS\ErrorInfo objects.

// Get the first error object
$error = $response->errors()->first();

// Return the error code
$error->code();

// Return the error message
$error->message();

// Return the clientReferenceList, see the official docs for more information
$error->clientReferenceList();

// Return the parcelIdList, see the official docs for more information
$error->parcelIdList();