1. Go to this page and download the library: Download softcreatr/php-openai-sdk 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/ */
softcreatr / php-openai-sdk example snippets
declare(strict_types=1);
leHttp\Psr7\HttpFactory;
use SoftCreatR\OpenAI\OpenAI;
$factory = new HttpFactory();
$openAI = new OpenAI(
requestFactory: $factory,
streamFactory: $factory,
uriFactory: $factory,
httpClient: new Client(['stream' => true]),
apiKey: (string) getenv('OPENAI_API_KEY'),
organization: (string) getenv('OPENAI_ORGANIZATION_ID'),
project: (string) getenv('OPENAI_PROJECT_ID'),
);
use const JSON_THROW_ON_ERROR;
$response = $openAI->createResponse([
'model' => 'gpt-5.4-mini',
'input' => 'Give me a one-sentence summary of PSR-18.',
]);
$result = json_decode((string) $response->getBody(), true, 512, JSON_THROW_ON_ERROR);
echo $result['output'][0]['content'][0]['text'];
$openAI->createEmbedding([
'model' => 'text-embedding-3-small',
'input' => 'A sentence to embed.',
]);