1. Go to this page and download the library: Download aisdk/xai 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/ */
aisdk / xai example snippets
use AiSdk\Generate;
use AiSdk\XAI;
$result = Generate::text()
->model(XAI::model('grok-4.3'))
->instructions('Write short, clear answers.')
->prompt('Explain closures in PHP.')
->run();
echo $result->text;
use AiSdk\Generate;
use AiSdk\XAI;
$result = Generate::image()
->model(XAI::model('grok-imagine-image-quality'))
->prompt('A clean app icon for a PHP AI SDK')
->aspectRatio('1:1')
->run();
$result->output->save(__DIR__.'/icon.png');
use AiSdk\Generate;
use AiSdk\XAI;
$result = Generate::speech()
->model(XAI::model('grok-voice'))
->input('Hello from xAI voice.')
->voice('eve')
->format('mp3')
->providerOptions('xai', [
'language' => 'auto',
'speed' => 1.1,
])
->run();
$result->output->save(__DIR__.'/speech.mp3');
use AiSdk\Content;
use AiSdk\Generate;
use AiSdk\XAI;
$result = Generate::transcription(Content::audio(__DIR__.'/meeting.mp3'))
->model(XAI::model('grok-transcribe'))
->providerOptions('xai', [
'language' => 'en',
'diarize' => true,
'keyterm' => ['PHP', 'AI SDK'],
])
->run();
echo $result->output->text;
use AiSdk\Live;
use AiSdk\Live\AudioDelta;
use AiSdk\Live\ResponseCompleted;
use AiSdk\Transport;
use AiSdk\XAI;
use function Amp\async;
$session = Live::voice()
->model(XAI::model('grok-voice-latest'))
->instructions('Be concise and helpful.')
->voice('eve')
->language('en')
->turnDetection('disabled')
->inputAudioFormat('pcm16')
->outputAudioFormat('pcm16')
->connect(Transport::auto());
$sender = async(function () use ($session): void {
while (! feof(STDIN)) {
$bytes = fread(STDIN, 4096);
if ($bytes !== false && $bytes !== '') {
$session->sendAudio($bytes);
}
}
$session->commitAudio();
$session->requestResponse();
});
foreach ($session->events() as $event) {
if ($event instanceof AudioDelta) {
fwrite(STDOUT, $event->bytes);
}
if ($event instanceof ResponseCompleted) {
break;
}
}
$sender->await();
$session->close();