Consume the Budbee API in PHP.
composer require setono/budbee-php-sdk
<?php
use Setono\Budbee\Client\Client;
use Setono\Budbee\DTO\Collection;
use Setono\Budbee\DTO\Box;
require_once '../vendor/autoload.php';
$client = new Client('API_KEY', 'API_SECRET');
// Set a logger to log more details about exceptions thrown
// $client->setLogger($logger);
// Enable sandbox mode to test your integration
// $client->setSandbox();
$boxes = $client
->boxes()
->getAvailableLockers('DK', '1159')
;
/** @var Box $box */
foreach ($boxes as $box) {
echo $box->name . " ($box->id)\n";
echo $box->address->street . "\n";
echo $box->address->postalCode . ' ' . $box->address->city . "\n";
echo $box->address->country . "\n\n";
}
will output something like:
Budbee CPH Office (BOX0012)
Ehlersvej 11
2900 Hellerup
DK
If the endpoint or method you want to call isn't present yet, you have two options: 1) Create a PR and add the missing parts or 2) use the generic request
method:
<?php
use Setono\Budbee\Client\Client;
require_once '../vendor/autoload.php';
$client = new Client('API_KEY', 'API_SECRET');
/** @var \Psr\Http\Message\ResponseInterface $response */
$response = $client->request(/** @var \Psr\Http\Message\RequestInterface $request */ $request);
Internally this library uses the CuyZ/Valinor library which is particularly well suited for turning API responses in DTOs. However, this library has some overhead and works best with a cache enabled.
When you instantiate the Client
you can provide a MapperBuilder
instance. Use this opportunity to set a cache:
<?php
use CuyZ\Valinor\Cache\FileSystemCache;
use CuyZ\Valinor\MapperBuilder;
use Setono\Budbee\Client\Client;
use Setono\Budbee\DTO\Collection;
use Setono\Budbee\DTO\Box;
require_once '../vendor/autoload.php';
$cache = new FileSystemCache('path/to/cache-directory');
$client = new Client('API_KEY', 'API_SECRET', (new MapperBuilder())->withCache($cache));
You can read more about it here: Valinor: Performance and caching.