PHP
There are two PHP paths into the Power API. Pick the one that matches your runtime:
| Runtime | Path | Auth |
|---|---|---|
| WordPress site | WP Engine AI Connector + WordPress AI Client SDK | Site connection — no key in code |
| Any other PHP (CLI, framework, worker) | Plain HTTP request | Bearer API key |
WordPress: AI Connector + AI Client SDK
Section titled “WordPress: AI Connector + AI Client SDK”For WordPress, the recommended path is the
WP Engine AI Connector plugin. Installing and
activating the connector registers a wpengine provider with the
WordPress AI Client SDK,
and the SDK handles auth, retries, and provider routing on your behalf.
Your plugin or theme code never touches an API key.
Prerequisites
Section titled “Prerequisites”- Install and activate the WP Engine AI Connector plugin.
- From Settings → WP Engine AI Connector, connect the site to a Power console project.
- In your plugin or theme, depend on the WordPress AI Client (bundled with WordPress 7.0+; install via Composer on earlier versions).
Generate text
Section titled “Generate text”The fluent API is the shortest path from prompt to result. The SDK discovers a suitable model from the registered providers, sends the request through the connector, and returns the candidate text.
<?phpdeclare( strict_types = 1 );
use WordPress\AiClient\AiClient;
$summary = AiClient::prompt( 'Summarise the speed of light in one sentence.' ) ->usingSystemInstruction( 'You are concise.' ) ->usingTemperature( 0.2 ) ->generateText();generateText() returns a plain string. For multiple candidates or
the full result object (token usage, finish reason, etc.) use
generateTexts() or generateTextResult() respectively.
Pin a specific model
Section titled “Pin a specific model”Auto-discovery picks any suitable model. When you need a specific one — for cost, latency, or capability reasons — name it explicitly. The SDK will fall through the list until it finds one that the connector exposes.
$summary = AiClient::prompt( 'Summarise the speed of light in one sentence.' ) ->usingProvider( 'wpengine' ) ->usingModelPreference( 'google/gemini-3.5-flash' ) ->generateText();Check availability before calling
Section titled “Check availability before calling”The connector reports itself as unavailable when the site is not connected. Gate user-facing features on that signal so a disconnected site degrades gracefully instead of throwing.
if ( ! AiClient::isConfigured( 'wpengine' ) ) { // Show an admin notice or fall back to a non-AI code path. return;}Generate an image
Section titled “Generate an image”Swap generateText() for generateImage() to produce an image file.
The SDK returns a File object — handle it as a stream and write it
through WordPress’s media APIs if you intend to persist it.
use WordPress\AiClient\AiClient;use WordPress\AiClient\Files\DTO\File;
$image = AiClient::prompt( 'A pixel-art lighthouse at sunset' ) ->usingProvider( 'wpengine' ) ->generateImage();
// $image is a WordPress\AiClient\Files\DTO\File — persist or stream it.Plain PHP: any non-WordPress runtime
Section titled “Plain PHP: any non-WordPress runtime”Outside WordPress — Laravel, Symfony, Slim, a CLI script — call the Power API directly. You’ll provide a Power console-issued API key and any HTTP client.
The example below uses Guzzle for
clarity, but any PSR-18 client (or even raw curl) works. Replace
POWER_API_KEY with however you load secrets in your environment —
never hard-code it.
List models
Section titled “List models”A cheap connectivity check. GET /v1/models requires no
authentication.
<?phpdeclare( strict_types = 1 );
use GuzzleHttp\Client;
$client = new Client( [ 'base_uri' => 'https://api.ai.wpengine.com', 'timeout' => 10,] );
$response = $client->get( '/v1/models' );$models = json_decode( (string) $response->getBody(), true );Chat completion
Section titled “Chat completion”Authenticated POST. The 30-second timeout gives the upstream model time to respond — tune it down for short prompts or up for longer-form generation.
<?phpdeclare( strict_types = 1 );
use GuzzleHttp\Client;use GuzzleHttp\Exception\RequestException;
$client = new Client( [ 'base_uri' => 'https://api.ai.wpengine.com', 'timeout' => 30, 'headers' => [ 'Authorization' => 'Bearer ' . getenv( 'POWER_API_KEY' ), 'Content-Type' => 'application/json', ],] );
try { $response = $client->post( '/v1/chat/completions', [ 'json' => [ 'model' => 'google/gemini-3.5-flash', 'messages' => [ [ 'role' => 'system', 'content' => 'You are concise.' ], [ 'role' => 'user', 'content' => 'Summarise the speed of light.' ], ], ], ] );
$body = json_decode( (string) $response->getBody(), true );} catch ( RequestException $e ) { // Inspect $e->getResponse() for the gateway's error envelope. throw $e;}See errors for the response envelope, and
rate limits for 429 handling.
Streaming
Section titled “Streaming”The WordPress HTTP API does not support Server-Sent Events natively, and the WordPress AI Client SDK does not yet expose a streaming generator. Until those land:
- Plain PHP: Guzzle’s
streamoption exposes the response body as a PSR-7 stream — read it line by line and split ondata: { ... }events. - WordPress: call the API from a Node companion service (see the Node sample for a streaming reader) and push tokens to the browser over your own channel.