PHP code example of php-client / openai
1. Go to this page and download the library: Download php-client/openai 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/ */
php-client / openai example snippets
use PhpClient\OpenAI\OpenAI;
$openai = new OpenAI(
baseUrl: 'https://api.openai.com',
token: 'YOUR_API_TOKEN',
);
$response = $openai->api->chat()->createChatCompletion(
model: 'gpt-4o',
messages: [
['role' => 'user', 'content' => 'Hello'],
],
);
echo $response->json(key: 'choices.0.message.content');
// or
foreach ($response->json(key: 'choices') as $choice) {
echo $choice['message']['content'];
}
bash
composer