Generate a PHP SDK for your API
Octri generates a PHP 8.2 client for your API with typed models on cURL, releases it to Packagist so Composer can install it, and regenerates it whenever your API changes.
Or upload a file: OpenAPI 3.0 or 3.1, Swagger 2.0, Postman or AsyncAPI.
composer require acme-api/sdkThe PHP API client your users get
What your users write once the PHP package is installed.
Two call styles come out of the same generator. class-namespaced is the default: one client object with operations grouped by resource, the shape most popular SDKs use and the one your API reference already shows. functions is the alternative: no client object, one configure call, and each operation imported on its own. It is one setting per language, and nothing else about the client changes with it.
Parameters are PHP 8 named arguments, so callers skip the optional ones by name. list is a reserved word in PHP, so a plain list operation is generated as list_() beside its listPaginated companion.
Packagist indexes your repository and Composer installs from its tags, so a PHP release is a git tag that Packagist picks up through its webhook.
<?php
use acme_api\Acme;
use acme_api\ClientAuthConfig;
use acme_api\ClientConfig;
$config = new ClientConfig();
$config->baseUrl = 'https://api.acme.com/v1';
$auth = new ClientAuthConfig();
$auth->bearer = getenv('ACME_TOKEN') ?: null;
$config->auth = $auth;
$acme = new Acme($config);
// listPaginated returns a lazy Generator: foreach pulls one page at a time.
foreach ($acme->users->listPaginated(limit: 100) as $user) {
echo $user->id, PHP_EOL;
}
$invoice = $acme->invoices->create(customerId: 'cus_123', amountCents: 4900);<?php
use acme_api\SdkConfig;
use acme_api\Users;
SdkConfig::configure($config);
foreach (Users::listUsersPaginated(limit: 100) as $user) {
echo $user->id, PHP_EOL;
}The PHP SDK at a glance
What a developer checks before adding the PHP package to a project.
- Runs on
- PHP 8.2+ with the curl extension
- Transport
- cURL
- Runtime dependencies
- None from Composer
- Models
- Classes with readonly typed properties
- Pagination
- Lazy Generator, consumed with foreach
- Ships to
- Packagist, installed with Composer
When a call fails
Each common status is its own exception class, so a catch block names the case it handles. A declared error model is decoded onto $e->data for an instanceof check.
<?php
use acme_api\SdkHttpError;
use acme_api\SdkRateLimitError;
try {
$acme->invoices->create(customerId: 'cus_123', amountCents: 4900);
} catch (SdkRateLimitError $e) {
// 429 after the built-in retries. Release the job back to the queue.
} catch (SdkHttpError $e) {
error_log("acme failed: {$e->statusCode} request {$e->requestId}");
}Configure your PHP SDK
PHP has no engine setting. The client talks cURL directly, so it adds no HTTP library to your users’ composer.lock.
Client style
class-namespaced (default), class, namespaced, functionsA client object with a property per resource, the new Acme($config) shape PHP developers expect. Functions become static methods on a class per resource.
Argument style
positional (default), objectTyped parameters that callers can pass by name, with optional ones defaulting to an unset marker so omitted and null stay distinct on the wire.
Doc comments
full (default), minimalFull writes PHPDoc with the array shapes PHPStan and PhpStorm read.
Method naming and namespacing work the same in every language: operations are grouped by your API’s tags or by path, and the namespace word is dropped from the method name unless you ask to keep the operationId whole.
Publishing to Packagist: it reads from your repository
Packagist stores metadata only and fetches the code from your git repository and its tags. That makes the PHP repository a required setting: without it there is nothing for Packagist to read. Set it per language before the first release, and register the vendor name on Packagist early, since vendor names are first come, first served.
acme-api/ sdk- Not possible: Release without a repository
- Not possible: Upload code to Packagist directly
- Still possible: Release by pushing a git tag
Retries, pagination and typed errors, built in
The parts nobody wants to hand-write, and the parts a hand-written client usually skips.
- Retries with exponential backoff and jitter, on the statuses that mean "try again", with a per-attempt timeout.
- Idempotency keys on the methods that need them, so a retried write does not become two.
- Auth wired in from your API’s security schemes, read from wherever your users keep secrets.
- Typed errors split by cause: a failed constraint, an HTTP status, a network failure and a timeout are four different things.
- Pagination that follows whichever style your API uses, cursor, offset, page number or next URL, without prefetching the collection.
- Server-sent events as a native stream, for the operations that stream.
- Error reporting to Octri’s monitoring, pre-wired and switched off until a consumer opts in.
Add your API
Upload an OpenAPI or Swagger file, paste a URL, connect the repository it lives in, or add endpoints in the editor.
Read the audit
Octri scores your API out of 10 on what a generator can do with it and names every missing schema and clashing operationId. 8 of the 14 rules come with a button that writes the fix.
Shape the client
Client style, argument style, namespacing, auth, retries and pagination, set once for the project and overridden per language or per endpoint.
Generate and read it
The package is generated, compiled and handed to you as source you can read before anyone installs it.
Publish, then keep publishing
Release to the registry under your own account. Every later change to your API regenerates the client, and the version number follows the version your API declares.
SDK generators for nine more languages
The same API in nine more languages, generated with the same settings at the same time.
PHP SDK generator questions
OpenAPI Generator’s PHP client defaults to Guzzle, with PSR-18 support in beta. Jane generates clients and DTOs from OpenAPI or Swagger. Both leave the Composer package and its releases to you. Octri generates a client on cURL with no Composer dependencies, releases it to Packagist, and regenerates it when your API changes.