1. Go to this page and download the library: Download grok-php/client 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/ */
grok-php / client example snippets
use GrokPHP\Client\Clients\GrokClient;
use GrokPHP\Client\Config\GrokConfig;
use GrokPHP\Client\Config\ChatOptions;
use GrokPHP\Client\Enums\Model;
// Initialize the client
$config = new GrokConfig('your-api-key');
$client = new GrokClient($config);
// Define messages
$messages = [
['role' => 'system', 'content' => 'You are an AI assistant.'],
['role' => 'user', 'content' => 'Tell me a joke!']
];
// Call API
$options = new ChatOptions(model: Model::GROK_2, temperature: 0.7, stream: false);
$response = $client->chat($messages, $options);
echo "🤖 AI Response: " . $response['choices'][0]['message']['content'];
use GrokPHP\Client\Clients\GrokClient;
use GrokPHP\Client\Config\GrokConfig;
use GrokPHP\Client\Config\ChatOptions;
use GrokPHP\Client\Enums\Model;
// Load API key from environment
$apiKey = getenv('GROK_API_KEY');
$config = new GrokConfig($apiKey);
$client = new GrokClient($config);
// Define messages
$messages = [
['role' => 'system', 'content' => 'You are a helpful assistant.'],
['role' => 'user', 'content' => 'How do black holes form?']
];
// Custom API settings
$options = new ChatOptions(
model: Model::GROK_2_LATEST,
temperature: 1.2,
stream: false
);
$response = $client->chat($messages, $options);
echo "🚀 AI Says: " . $response['choices'][0]['message']['content'];