Skip to content
WP EngineDocumentation

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

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.

  1. Install and activate the WP Engine AI Connector plugin.
  2. From Settings → WP Engine AI Connector, connect the site to a Power console project.
  3. In your plugin or theme, depend on the WordPress AI Client (bundled with WordPress 7.0+; install via Composer on earlier versions).

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.

<?php
declare( 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.

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();

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;
}

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.

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.

A cheap connectivity check. GET /v1/models requires no authentication.

<?php
declare( 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 );

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.

<?php
declare( 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.

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 stream option exposes the response body as a PSR-7 stream — read it line by line and split on data: { ... } 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.

Last updated: