pwinty/php-pwinty
PHP client library for the Pwinty photo printing API. Create and manage orders, upload photos, set shipping addresses, and check order status from your Laravel/PHP apps. Simple wrapper around Pwinty endpoints for quick print product integration.
Installation
composer require pwinty/php-pwinty
Verify the package is autoloaded in composer.json under "autoload": { "psr-4": { ... } }.
First Use Case: Authentication Initialize the client with your API credentials:
use Pwinty\Client;
$client = new Client('YOUR_API_KEY', 'YOUR_API_SECRET');
Store credentials securely in .env (e.g., PWINTY_API_KEY, PWINTY_API_SECRET).
First API Call: Fetch Orders
$orders = $client->orders()->all();
Check the Pwinty API docs for available endpoints.
Order Management
// Fetch a single order
$order = $client->orders()->find(123);
// Create a new order
$newOrder = $client->orders()->create([
'product_id' => 456,
'quantity' => 2,
]);
Service Provider to bind the client for dependency injection:
$this->app->singleton(Client::class, function ($app) {
return new Client(
$app['config']['services.pwinty.key'],
$app['config']['services.pwinty.secret']
);
});
Webhook Handling
Route::post('/pwinty/webhook', function (Request $request) {
$client = app(Client::class);
$webhook = $client->webhooks()->validate($request->input());
if ($webhook->isValid()) {
// Process the webhook (e.g., update inventory)
}
});
config/services.php:
'pwinty' => [
'webhook_secret' => env('PWINTY_WEBHOOK_SECRET'),
],
Batch Operations
$orders = $client->orders()->all(['per_page' => 100]);
while ($orders->hasMore()) {
$orders = $client->orders()->nextPage($orders);
}
$client->setLogger(new Monolog\Logger('pwinty'));
$mock = $this->createMock(Client::class);
$mock->method('orders')->willReturnSelf();
$mock->method('all')->willReturn([...]);
Deprecated Package
Rate Limiting
try {
$response = $client->orders()->all();
} catch (RateLimitExceededException $e) {
sleep($e->getRetryAfter());
retry();
}
Webhook Validation
if (!$client->webhooks()->validate($request->input())) {
abort(403, 'Invalid webhook signature');
}
$client->setDebug(true); // Logs raw API requests/responses
4xx/5xx errors gracefully:
try {
$client->orders()->find(123);
} catch (ApiException $e) {
Log::error("Pwinty API Error: {$e->getMessage()}", ['response' => $e->getResponse()]);
}
Custom Endpoints
class CustomClient extends Client {
public function customEndpoint() {
return $this->request('GET', '/custom-path');
}
}
Middleware
$client->getMiddleware()->push(function ($request) {
$request->headers->set('X-Custom-Header', 'value');
});
Configuration
$client = new Client($key, $secret, 'https://custom.pwinty.com/api');
How can I help you explore Laravel packages today?