PHP code example of smart-dato / gls-authenticator
1. Go to this page and download the library: Download smart-dato/gls-authenticator 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/ */
use SmartDato\GlsAuthenticator\Facades\GlsAuthenticator;
$token = GlsAuthenticator::getToken();
// Use the token
$authHeader = $token->toAuthorizationHeader(); // "Bearer eyJ..."
// Each tenant has their own credentials
$tenant = Tenant::current();
$token = GlsAuthenticator::withCredentials(
$tenant->gls_client_id,
$tenant->gls_client_secret
)->getToken();
// Use sandbox
$token = GlsAuthenticator::environment('sandbox')->getToken();
// Use production
$token = GlsAuthenticator::environment('production')->getToken();
// Clear token for current credentials
GlsAuthenticator::clearCache();
// Clear all cached tokens
GlsAuthenticator::clearAllTokens();
if (GlsAuthenticator::isConfigured()) {
$token = GlsAuthenticator::getToken();
} else {
// Prompt user to configure credentials
}
use SmartDato\GlsAuthenticator\Exceptions\MissingCredentialsException;
use SmartDato\GlsAuthenticator\Exceptions\InvalidCredentialsException;
use SmartDato\GlsAuthenticator\Exceptions\TokenRequestException;
try {
$token = GlsAuthenticator::getToken();
} catch (MissingCredentialsException $e) {
// No credentials configured
logger()->error('GLS credentials not configured');
} catch (InvalidCredentialsException $e) {
// Invalid client_id or client_secret
logger()->error('Invalid GLS credentials', ['errors' => $e->errors]);
} catch (TokenRequestException $e) {
// API request failed (network error, server error, etc.)
logger()->error('GLS API request failed', ['message' => $e->getMessage()]);
}
use Illuminate\Support\Facades\Http;
$token = GlsAuthenticator::getToken();
$response = Http::withToken($token->token)
->post('https://api.gls-group.net/parcel/v1/shipments', [
// Your shipment data
]);
namespace App\Http\Middleware;
use Closure;
use SmartDato\GlsAuthenticator\Facades\GlsAuthenticator;
class SetGlsCredentials
{
public function handle($request, Closure $next)
{
$tenant = $request->user()->tenant;
// Store token for later use
$request->attributes->set('gls_token',
GlsAuthenticator::withCredentials(
$tenant->gls_client_id,
$tenant->gls_client_secret
)->getToken()
);
return $next($request);
}
}
namespace App\Services;
use SmartDato\GlsAuthenticator\Facades\GlsAuthenticator;
use Illuminate\Support\Facades\Http;
class GlsParcelService
{
protected $token;
public function __construct()
{
$this->token = GlsAuthenticator::getToken();
}
public function createShipment(array $data)
{
return Http::withToken($this->token->token)
->post('https://api.gls-group.net/parcel/v1/shipments', $data)
->json();
}
public function trackParcel(string $parcelNumber)
{
return Http::withToken($this->token->token)
->get("https://api.gls-group.net/parcel/v1/tracking/{$parcelNumber}")
->json();
}
}
use Illuminate\Support\Facades\Http;
use SmartDato\GlsAuthenticator\Facades\GlsAuthenticator;
test('can authenticate with GLS API', function () {
Http::fake([
'api-sandbox.gls-group.net/*' => Http::response([
'token_type' => 'Bearer',
'access_token' => 'test_token_123',
'expires_in' => 14400,
]),
]);
$token = GlsAuthenticator::getToken();
expect($token->token)->toBe('test_token_123');
expect($token->tokenType)->toBe('Bearer');
});