How to Integrate Grok AI into PHP Apps with the Grok‑PHP Client

This guide explains how to install, configure, and use the Grok‑PHP client to call Grok AI models from PHP, covering basic usage, advanced options, available model enums, and streaming responses with code examples.

Open Source Tech Hub
Open Source Tech Hub
Open Source Tech Hub
How to Integrate Grok AI into PHP Apps with the Grok‑PHP Client

Grok is a generative AI chatbot developed by xAI, launched in 2023 and updated to Grok 3 in February 2025, featuring humor and direct X (formerly Twitter) integration.

The Grok‑PHP library provides a lightweight, framework‑agnostic client for PHP developers to access Grok AI, supporting Laravel, Symfony (upcoming), and plain PHP.

Installation

composer require grok-php/client

Quick Start

Basic Usage

use GrokPHP\Client\Clients\GrokClient;
use GrokPHP\Client\Config\GrokConfig;
use GrokPHP\Client\Config\ChatOptions;
use GrokPHP\Client\Enums\Model;

$config = new GrokConfig('your-api-key');
$client = new GrokClient($config);

$messages = [
    ['role' => 'system', 'content' => 'You are an AI assistant.'],
    ['role' => 'user', 'content' => 'Tell me a joke!']
];

$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'];

Advanced Configuration

use GrokPHP\Client\Clients\GrokClient;
use GrokPHP\Client\Config\GrokConfig;
use GrokPHP\Client\Config\ChatOptions;
use GrokPHP\Client\Enums\Model;

$apiKey = getenv('GROK_API_KEY');
$config = new GrokConfig($apiKey);
$client = new GrokClient($config);

$messages = [
    ['role' => 'system', 'content' => 'You are a helpful assistant.'],
    ['role' => 'user', 'content' => 'How do black holes form?']
];

$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'];

Available Grok AI Models

The package includes a Model enum that maps to the API model names, each optimized for different use cases: Model::GROK_VISION_BETA – grok-vision-beta : experimental vision‑enabled model Model::GROK_2_VISION – grok-2-vision : advanced multi‑modal vision model Model::GROK_2_VISION_LATEST – grok-2-vision-latest : latest vision iteration Model::GROK_2_VISION_1212 – grok-2-vision-1212 : enhanced vision with performance gains Model::GROK_2_1212 – grok-2-1212 : optimized chat model Model::GROK_2 – grok-2 : default general‑purpose model Model::GROK_2_LATEST – grok-2-latest : latest Grok‑2 iteration Model::GROK_BETA – grok-beta : experimental beta model

Streaming Responses

Grok API supports streaming for real‑time interaction. Enable it by setting stream:true in ChatOptions:

$options = new ChatOptions(model: Model::GROK_2, temperature: 0.7, stream: true);
$response = $client->chat($messages, $options);

Streaming is useful for chatbots, live applications, and CLI assistants.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

AIStreamingPHPAPIGrokClient Library
Open Source Tech Hub
Written by

Open Source Tech Hub

Sharing cutting-edge internet technologies and practical AI resources.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.