PHP code example of openai-php / laravel

1. Go to this page and download the library: Download openai-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/ */

    

openai-php / laravel example snippets


use OpenAI\Laravel\Facades\OpenAI;

$response = OpenAI::responses()->create([
    'model' => 'gpt-5',
    'input' => 'Hello!',
]);

echo $response->outputText; // Hello! How can I assist you today?

use OpenAI\Laravel\Facades\OpenAI;
use OpenAI\Responses\Responses\CreateResponse;

OpenAI::fake([
    CreateResponse::fake([
        'choices' => [
            [
                'text' => 'awesome!',
            ],
        ],
    ]),
]);

$response = OpenAI::responses()->create([
    'model' => 'gpt-5',
    'input' => 'PHP is ',
]);

expect($response->outputText)->toBe('awesome!');

// assert completion create request was sent
OpenAI::assertSent(Responses::class, function (string $method, array $parameters): bool {
    return $method === 'create' &&
        $parameters['model'] === 'gpt-5' &&
        $parameters['prompt'] === 'PHP is ';
});
bash
composer 
bash
php artisan openai:install