<?php
// Replace with your actual API key from https://console.x.ai
$apiKey = 'xai-sEIRKlnxuibfkWEGcBbNEcyMdiTWwG6BQPHyCO9wTxw25U0Gaa3fM6qLhYoCBOJ6URY3c4rEUkJCnSHW';
// API endpoint
$url = 'https://api.x.ai/v1/chat/completions';
// Request payload
$data = [
'messages' => [
[
'role' => 'user',
'content' => 'What is the meaning of life, the universe, and everything?'
]
],
'model' => 'grok-3-mini',
'stream' => false,
'temperature' => 0.7
];
// Initialize cURL
$ch = curl_init($url);
// Set cURL options
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type: application/json',
'Authorization: Bearer ' . $apiKey
]);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
// Execute request and get response
$response = curl_exec($ch);
// Check for cURL errors
if (curl_errno($ch)) {
echo 'cURL Error: ' . curl_error($ch);
} else {
// Decode and print response
$decodedResponse = json_decode($response, true);
if ($decodedResponse === null) {
echo 'Error decoding response: ' . $response;
} else {
print_r($decodedResponse);
}
}
// Close cURL
curl_close($ch);
?>