PHP SDK

Official PHP SDK for HINOW API with Laravel integration

PHP SDK

Install and configure the HINOW PHP SDK to integrate over 100 AI models into PHP applications with PSR-18 compatibility.


The HINOW PHP SDK provides full access to the HINOW REST API with PSR-18 HTTP client support, streaming, and comprehensive error handling.

> Note: For API resource documentation with code examples, see the API Reference. This page covers PHP SDK-specific features and configurations.

Installation

bash
composer require hinow-ai/hinow-php

Requirements

  • PHP 8.1+
  • Composer

Configuration

Basic Setup

php
<?php

use Hinow\Hinow;

\$client = new Hinow('your-api-key');

Environment Variable

bash
export HINOW_API_KEY=your-api-key

php
// API key loaded automatically from environment
\$client = new Hinow();

Advanced Configuration

php
\$client = new Hinow(
    apiKey: 'your-api-key',
    baseUrl: 'https://api.hinow.ai',
    timeout: 120,
    maxRetries: 3
);

Basic Usage

Chat Completions

php
<?php

use Hinow\Hinow;

\$client = new Hinow();

\$response = \$client->chat->completions->create([
    'model' => 'gpt-4o',
    'messages' => [
        ['role' => 'system', 'content' => 'You are a helpful assistant.'],
        ['role' => 'user', 'content' => 'What is the capital of France?']
    ],
    'temperature' => 0.7,
    'max_tokens' => 1024,
]);

echo \$response->choices[0]->message->content;

Using Different Models

php
// OpenAI GPT-4o
\$response = \$client->chat->completions->create([
    'model' => 'gpt-4o',
    'messages' => [['role' => 'user', 'content' => 'Explain machine learning']],
]);

// Anthropic Claude
\$response = \$client->chat->completions->create([
    'model' => 'claude-sonnet-4-20250514',
    'messages' => [['role' => 'user', 'content' => 'Explain machine learning']],
]);

// DeepSeek
\$response = \$client->chat->completions->create([
    'model' => 'deepseek-ai/deepseek-v3.2',
    'messages' => [['role' => 'user', 'content' => 'Explain machine learning']],
]);

Streaming

php
\$stream = \$client->chat->completions->create([
    'model' => 'gpt-4o',
    'messages' => [['role' => 'user', 'content' => 'Write a story about a robot']],
    'stream' => true,
]);

foreach (\$stream as \$chunk) {
    \$content = \$chunk->choices[0]->delta->content ?? '';
    echo \$content;
}

Function Calling (Tool Use)

php
\$response = \$client->chat->completions->create([
    'model' => 'gpt-4o',
    'messages' => [
        ['role' => 'user', 'content' => 'What is the weather in New York?']
    ],
    'tools' => [
        [
            'type' => 'function',
            'function' => [
                'name' => 'get_weather',
                'description' => 'Get the current weather for a location',
                'parameters' => [
                    'type' => 'object',
                    'properties' => [
                        'location' => [
                            'type' => 'string',
                            'description' => 'City and state, e.g., New York, NY'
                        ],
                        'unit' => [
                            'type' => 'string',
                            'enum' => ['celsius', 'fahrenheit']
                        ]
                    ],
                    'required' => ['location']
                ]
            ]
        ]
    ],
    'tool_choice' => 'auto'
]);

\$message = \$response->choices[0]->message;

if (\$message->tool_calls) {
    foreach (\$message->tool_calls as \$toolCall) {
        echo "Function: {\$toolCall->function->name}\n";
        echo "Arguments: {\$toolCall->function->arguments}\n";
    }
}

Image Generation

php
\$response = \$client->images->generate([
    'model' => 'black-forest-labs/flux-1-schnell',
    'prompt' => 'A programmer cat wearing glasses, cartoon style',
    'size' => '1024x1024',
    'quality' => 'hd',
]);

foreach (\$response->data as \$image) {
    echo "URL: {\$image->url}\n";
}

Embeddings

php
\$response = \$client->embeddings->create([
    'model' => 'BAAI/bge-base-en-v1.5',
    'input' => 'Machine learning is fascinating',
]);

\$embedding = \$response->data[0]->embedding;
echo "Dimensions: " . count(\$embedding) . "\n";

Error Handling

php
use Hinow\Hinow;
use Hinow\Exceptions\AuthenticationException;
use Hinow\Exceptions\InsufficientBalanceException;
use Hinow\Exceptions\RateLimitException;
use Hinow\Exceptions\BadRequestException;
use Hinow\Exceptions\HinowException;

try {
    \$response = \$client->chat->completions->create([
        'model' => 'nonexistent-model',
        'messages' => [['role' => 'user', 'content' => 'Hello']],
    ]);
} catch (AuthenticationException \$e) {
    echo "Invalid API key\n";
} catch (InsufficientBalanceException \$e) {
    echo "Insufficient balance\n";
} catch (RateLimitException \$e) {
    echo "Rate limit reached. Retry after: {\$e->retryAfter}\n";
} catch (BadRequestException \$e) {
    echo "Invalid request: {\$e->getMessage()}\n";
} catch (HinowException \$e) {
    echo "API Error [{\$e->statusCode}]: {\$e->getMessage()}\n";
}

Laravel Integration

php
// config/services.php
'hinow' => [
    'api_key' => env('HINOW_API_KEY'),
],

// AppServiceProvider.php
use Hinow\Hinow;

public function register()
{
    \$this->app->singleton(Hinow::class, function () {
        return new Hinow(config('services.hinow.api_key'));
    });
}

// In your controller
use Hinow\Hinow;

class ChatController extends Controller
{
    public function __construct(private Hinow \$client) {}

    public function chat(Request \$request)
    {
        \$response = \$this->client->chat->completions->create([
            'model' => 'gpt-4o',
            'messages' => [['role' => 'user', 'content' => \$request->message]],
        ]);

        return response()->json([
            'message' => \$response->choices[0]->message->content
        ]);
    }
}

Additional Resources