Skip to main content

PHP Integration

Also available in: Python · Node.js · Go

The "gateway base URL" is the address of your Observra Gateway (https://gateway.observra.in) - your app sends requests here instead of directly to OpenAI, Anthropic, or whichever provider you use. The "gateway key" is a per-Application credential you generate in the Observra Dashboard; it authenticates your app to the gateway and is separate from your provider's own API key, which you still keep and send yourself. See Quickstart and Gateway Authentication if you haven't set these up yet. Note: the tested PHP examples below don't include trace propagation - trace passing wasn't part of the source scripts for PHP.

Try it now

Paste in a gateway URL, your gateway key, and a provider key to send a real request straight from this page - no code required yet.

Loading playground...

Setup

Assume these values already exist in your app (load them from environment variables or your framework's config, don't hardcode them):

$gatewayBase = 'https://gateway.observra.in';
$gatewayKey = 'observra_gateway_key';
$providerKey = 'provider_api_key';

Raw HTTP requests (cURL)

Every raw cURL call adds X-Gateway-Key for the gateway and X-Provider-Key for the upstream provider, then POSTs to the gateway route.

$payload = [
'model' => 'gpt-4o-mini',
'messages' => [
['role' => 'user', 'content' => 'Say hello in one line.'],
],
];

$ch = curl_init($gatewayBase.'/openai/v1/chat/completions');
curl_setopt_array($ch, [
CURLOPT_POST => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => [
'Content-Type: application/json',
'X-Gateway-Key: '.$gatewayKey,
'X-Provider-Key: '.$providerKey,
],
CURLOPT_POSTFIELDS => json_encode($payload, JSON_UNESCAPED_SLASHES),
CURLOPT_TIMEOUT => 60,
]);

$data = json_decode(curl_exec($ch), true);
$text = $data['choices'][0]['message']['content'] ?? '';

Native SDKs

Install:

composer require openai-php/client anthropic-ai/sdk google-gemini-php/client guzzlehttp/guzzle
$client = OpenAI::factory()
->withApiKey($providerKey)
->withBaseUri($gatewayBase.'/openai/v1')
->withHttpHeader('X-Gateway-Key', $gatewayKey)
->withHttpHeader('X-Provider-Key', $providerKey)
->make();

$response = $client->chat()->create([
'model' => 'gpt-4o-mini',
'messages' => [
['role' => 'user', 'content' => 'Say hello in one line.'],
],
]);

$text = $response->choices[0]->message->content ?? '';

Agent frameworks & SDKs

More advanced: full PHP frameworks that wrap one or more providers behind their own client abstraction. Each still needs X-Gateway-Key and X-Provider-Key set on the underlying HTTP client, plus X-Azure-Endpoint/X-Azure-Api-Version for Azure.

$httpFactory->globalOptions([
'headers' => [
'X-Gateway-Key' => $gatewayKey,
'X-Provider-Key' => $providerKey,
],
]);

$agent = new Laravel\Ai\AnonymousAgent('Reply with a short greeting only.', [], []);

$response = $provider->prompt(new Laravel\Ai\Prompts\AgentPrompt(
$agent,
'Say hello in one line.',
[],
$provider,
$model,
60,
));

$text = $response->text;

OpenAI provider setup:

$config = [
'driver' => 'openai',
'key' => $providerKey,
'url' => $gatewayBase.'/openai/v1',
'models' => ['text' => ['default' => 'gpt-4o-mini']],
];

$provider = new Laravel\Ai\Providers\OpenAiProvider(
new Laravel\Ai\Gateway\OpenAi\OpenAiGateway($events),
$config,
$events,
);
$model = 'gpt-4o-mini';

Anthropic, Gemini, Groq, Ollama, and OpenRouter follow the same pattern with their matching Laravel\Ai\Providers\*Provider class and url set to $gatewayBase.'/<provider>/v1' (or /v1beta for Gemini, no suffix for Ollama). Azure needs the extra headers on $httpFactory plus 'driver' => 'azure', 'url' => $gatewayBase.'/azure', and 'api_version'/'deployment' keys.

Notes

  • Use X-Gateway-Key for Observra Gateway auth, X-Provider-Key for the upstream provider's own auth.
  • Azure also needs X-Azure-Endpoint and X-Azure-Api-Version headers.
  • SDK/framework base URLs usually stop at the provider's base path; the SDK appends the endpoint path (e.g. /chat/completions) internally.

Next steps