PHP code example of grok-php / laravel

1. Go to this page and download the library: Download grok-php/laravel 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 / laravel example snippets


use GrokPHP\Laravel\Facades\GrokAI;
use GrokPHP\Client\Config\ChatOptions;
use GrokPHP\Client\Enums\Model;

$response = GrokAI::chat(
    [['role' => 'user', 'content' => 'Hello Grok!']],
    new ChatOptions(model: Model::GROK_2)
);

echo $response->content();

$response = GrokAI::vision()->analyze(
    'https://example.com/sample.jpg',
    'Describe this image'
);

echo $response->content();

use GrokPHP\Client\Exceptions\GrokException;

try {
    $response = GrokAI::chat(
        [['role' => 'user', 'content' => 'Hello!']]
    );
    echo $response->content();
} catch (GrokException $e) {
    echo "Error: " . $e->getMessage();
}

$response = GrokAI::chat(
    [['role' => 'user', 'content' => 'Tell me a story']],
    new ChatOptions(model: Model::GROK_2, stream: true)
);
sh
php artisan grok:install
xml
<php>
    <env name="GROK_API_KEY" value="your-grok-api-key"/>
</php>