<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
gigerit / laravel-swiss-post-postcard-api-client example snippets
use Gigerit\PostcardApi\PostcardApi;
use Gigerit\PostcardApi\DTOs\Address\RecipientAddress;
class PostcardController extends Controller
{
public function __construct(private PostcardApi $postcardApi) {}
public function sendPostcard()
{
// Create recipient address
$recipient = new RecipientAddress(
street: 'Musterstrasse',
zip: '8000',
city: 'Zürich',
country: 'CH',
firstname: 'John',
lastname: 'Doe',
houseNr: '123'
);
// Send postcard
$result = $this->postcardApi->postcards()->createComplete(
recipientAddress: $recipient,
imagePath: storage_path('postcards/my-image.jpg'),
senderText: 'Hello from Laravel!'
);
return response()->json(['cardKey' => $result->cardKey]);
}
}
use Gigerit\PostcardApi\Facades\PostcardApi;
use Gigerit\PostcardApi\DTOs\Address\RecipientAddress;
// Check campaign quota
$stats = PostcardApi::campaigns()->getDefaultCampaignStatistics();
if ($stats->freeToSendPostcards === 0) {
throw new Exception('No postcards remaining in campaign');
}
// Create and send postcard
$recipient = new RecipientAddress(/* ... */);
$result = PostcardApi::postcards()->createComplete(
recipientAddress: $recipient,
imagePath: 'path/to/image.jpg'
);
use Gigerit\PostcardApi\PostcardApi;
use Gigerit\PostcardApi\Connectors\SwissPostConnector;
// Auto-authenticate using configured OAuth2 credentials
$api = new PostcardApi();
// Or provide specific access token
$api = new PostcardApi('your_access_token_here');
// Using Saloon's OAuth2 directly
$connector = new SwissPostConnector();
$authenticator = $connector->getAccessToken(); // Uses client credentials grant
$connector->authenticate($authenticator);