1. Go to this page and download the library: Download displace/xai-sdk-php library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
displace / xai-sdk-php example snippets
use Displace\XaiSdk\XaiClient;
// Uses XAI_API_KEY environment variable
$client = new XaiClient();
// Or explicitly pass the API key
$client = new XaiClient(apiKey: 'your-api-key');
use Displace\XaiSdk\XaiClient;
use function Displace\XaiSdk\Chat\system;
use function Displace\XaiSdk\Chat\user;
$client = new XaiClient();
$chat = $client->chat->create(
model: 'grok-3',
messages: [
system('You are a helpful assistant.'),
]
);
// Interactive chat loop
while (true) {
echo "You: ";
$prompt = trim(fgets(STDIN));
if (strtolower($prompt) === 'exit') {
break;
}
$chat->append(user($prompt));
$response = $chat->sample();
echo "Grok: {$response->getContent()}\n";
$chat->append($response);
}
use Displace\XaiSdk\XaiClient;
use function Displace\XaiSdk\Chat\user;
$client = new XaiClient();
$chat = $client->chat->create(model: 'grok-3');
$chat->append(user('Explain quantum computing in simple terms.'));
echo "Grok: ";
foreach ($chat->stream() as [$response, $chunk]) {
echo $chunk->content;
}
echo "\n";
// Append the final response to continue the conversation
$chat->append($response);
use Displace\XaiSdk\XaiClient;
use function Displace\XaiSdk\Chat\image;
use function Displace\XaiSdk\Chat\user;
$client = new XaiClient();
$chat = $client->chat->create(model: 'grok-2-vision');
$chat->append(
user(
'What do these images have in common?',
image('https://example.com/image1.jpg', 'high'),
image('https://example.com/image2.jpg', 'high')
)
);
$response = $chat->sample();
echo "Grok: {$response->getContent()}\n";
use Displace\XaiSdk\XaiClient;
$client = new XaiClient(
apiKey: 'your-api-key', // API key (or use XAI_API_KEY env var)
baseUrl: 'https://api.x.ai/v1', // API base URL
timeout: 120, // Request timeout in seconds
);
$chat = $client->chat->create(
model: 'grok-3', // Model to use
messages: [...], // Initial messages
temperature: 0.7, // Sampling temperature (0.0-2.0)
maxTokens: 1024, // Maximum tokens in response
topP: 0.9, // Nucleus sampling parameter
frequencyPenalty: 0.0, // Frequency penalty (-2.0-2.0)
presencePenalty: 0.0, // Presence penalty (-2.0-2.0)
tools: [...], // Tool/function definitions
responseFormat: [...], // Structured output schema
reasoningEffort: 'high', // For reasoning models: 'low' or 'high'
);
use Displace\XaiSdk\Telemetry\Telemetry;
use Displace\XaiSdk\XaiClient;
$telemetry = new Telemetry();
$telemetry->setupConsoleExporter();
$client = new XaiClient();
// All API calls will now emit traces to the console
use Displace\XaiSdk\Telemetry\Telemetry;
$telemetry = new Telemetry();
$telemetry->setupOtlpExporter(
endpoint: 'https://your-observability-platform.com/traces',
headers: ['Authorization' => 'Bearer your-token'],
);
use Displace\XaiSdk\Exceptions\AuthenticationException;
use Displace\XaiSdk\Exceptions\RateLimitException;
use Displace\XaiSdk\Exceptions\ApiException;
use Displace\XaiSdk\Exceptions\XaiException;
try {
$response = $chat->sample();
} catch (AuthenticationException $e) {
// Invalid or missing API key
echo "Authentication failed: {$e->getMessage()}\n";
} catch (RateLimitException $e) {
// Rate limit exceeded - implement backoff
echo "Rate limited. Retry after: {$e->getRetryAfter()} seconds\n";
} catch (ApiException $e) {
// API returned an error
echo "API error ({$e->getHttpStatusCode()}): {$e->getMessage()}\n";
} catch (XaiException $e) {
// Base exception for all SDK errors
echo "Error: {$e->getMessage()}\n";
}