PHPackages                             sgraaf/openai-php - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [API Development](/categories/api)
4. /
5. sgraaf/openai-php

ActiveLibrary[API Development](/categories/api)

sgraaf/openai-php
=================

A thin PHP wrapper for the OpenAI API built upon libcurl.

0.1.6(3y ago)36.9k↓13.3%2[1 PRs](https://github.com/sgraaf/openai-php/pulls)1MITPHPPHP &gt;=8.0.0

Since Feb 15Pushed 2mo ago1 watchersCompare

[ Source](https://github.com/sgraaf/openai-php)[ Packagist](https://packagist.org/packages/sgraaf/openai-php)[ Docs](https://github.com/sgraaf/openai-php)[ RSS](/packages/sgraaf-openai-php/feed)WikiDiscussions main Synced 2d ago

READMEChangelogDependencies (2)Versions (9)Used By (1)

OpenAI PHP
==========

[](#openai-php)

[![Packagist Version](https://camo.githubusercontent.com/6101dfff310c1820963f9e594b112b2daa8163daad9309aac16b0c56eaf4e26a/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f7367726161662f6f70656e61692d706870)](https://packagist.org/packages/sgraaf/openai-php)[![Packagist PHP Version](https://camo.githubusercontent.com/1eba9dcaba81dd1db2f4221e6ea65ed7e30ed386dfcd3bdc6caa28f39ececf0b/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f646570656e64656e63792d762f7367726161662f6f70656e61692d7068702f706870)](https://img.shields.io/packagist/dependency-v/sgraaf/openai-php/php)[![pre-commit.ci status](https://camo.githubusercontent.com/5f1e049cfacca0ca270d023392f9647e423ce178e1df2e9668755c7e051f8b8a/68747470733a2f2f726573756c74732e7072652d636f6d6d69742e63692f62616467652f6769746875622f7367726161662f6f70656e61692d7068702f6d61696e2e737667)](https://results.pre-commit.ci/latest/github/sgraaf/openai-php/main)[![Packagist License](https://camo.githubusercontent.com/e4756352980a3cad8d032071fa2ab263934de3c9a801e030d37cd6f0fac49a59/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f7367726161662f6f70656e61692d706870)](./LICENSE)

A thin PHP wrapper for the OpenAI API built upon `libcurl`.

Installation
------------

[](#installation)

You can install OpenAI PHP via [Composer](https://getcomposer.org/):

```
composer require sgraaf/openai-php
```

Usage
-----

[](#usage)

### Models

[](#models)

#### List models

[](#list-models)

Lists the currently available models, and provides basic information about each one such as the owner and availability. See the [OpenAI docs](https://platform.openai.com/docs/api-reference/models/list) for more information.

```
// initialize the client
$client = new OpenAI\Client('YOUR_OPENAI_API_KEY');

// lists the currently available models
var_dump($client->listModels());
```

#### Retrieve model

[](#retrieve-model)

Retrieves a model instance, providing basic information about the model such as the owner and permissioning. See the [OpenAI docs](https://platform.openai.com/docs/api-reference/models/retrieve) for more information.

```
// initialize the client
$client = new OpenAI\Client('YOUR_OPENAI_API_KEY');

// retrieves a model instance
var_dump($client->retrieveModel(model: 'text-davinci-003'));
```

### Completions

[](#completions)

#### Create completion

[](#create-completion)

Creates a completion for the provided prompt and parameters. See the [OpenAI docs](https://platform.openai.com/docs/api-reference/completions/create) for more information.

```
// initialize the client
$client = new OpenAI\Client('YOUR_OPENAI_API_KEY');

// creates a completion for the provided prompt and parameters
var_dump($client->createCompletion(model: 'text-davinci-003', prompt: 'Say this is a test', max_tokens: 7, temperature: 0));
```

### Chat

[](#chat)

#### Create chat completion

[](#create-chat-completion)

Creates a completion for the chat message. See the [OpenAI docs](https://platform.openai.com/docs/api-reference/chat/create) for more information.

```
// initialize the client
$client = new OpenAI\Client('YOUR_OPENAI_API_KEY');

// creates a completion for the chat message
var_dump($client->createChatCompletion(model: 'gpt-3.5-turbo', messages: [['role' => 'user', 'content' => 'Hello!']]));
```

### Edits

[](#edits)

#### Create edit

[](#create-edit)

Creates a new edit for the provided input, instruction, and parameters. See the [OpenAI docs](https://platform.openai.com/docs/api-reference/edits/create) for more information.

```
// initialize the client
$client = new OpenAI\Client('YOUR_OPENAI_API_KEY');

// creates a new edit for the provided input, instruction, and parameters
var_dump($client->createEdit(model: 'text-davinci-003', input: 'What day of the wek is it?', instruction: 'Fix the spelling mistakes'));
```

### Images

[](#images)

#### Create image

[](#create-image)

Creates an image given a prompt. See the [OpenAI docs](https://platform.openai.com/docs/api-reference/images/create) for more information.

```
// initialize the client
$client = new OpenAI\Client('YOUR_OPENAI_API_KEY');

// creates an image given a prompt
var_dump($client->createImage(prompt: 'A cute baby sea otter', n: 2, size: '1024x1024'));
```

#### Create image edit

[](#create-image-edit)

Creates an edited or extended image given an original image and a prompt. See the [OpenAI docs](https://platform.openai.com/docs/api-reference/images/create-edit) for more information.

```
// initialize the client
$client = new OpenAI\Client('YOUR_OPENAI_API_KEY');

// creates an edited or extended image given an original image and a prompt
var_dump($client->createImageEdit(image: 'otter.png', mask: 'mask.png', prompt: 'A cute baby sea otter wearing a beret', n: 2, size: '1024x1024'));
```

#### Create image variation

[](#create-image-variation)

Creates a variation of a given image. See the [OpenAI docs](https://platform.openai.com/docs/api-reference/images/create-variation) for more information.

```
// initialize the client
$client = new OpenAI\Client('YOUR_OPENAI_API_KEY');

// creates a variation of a given image
var_dump($client->createImageVariation(image: 'otter.png', n: 2, size: '1024x1024'));
```

### Embeddings

[](#embeddings)

#### Create embeddings

[](#create-embeddings)

Creates an embedding vector representing the input text. See the [OpenAI docs](https://platform.openai.com/docs/api-reference/embeddings/create) for more information.

```
// initialize the client
$client = new OpenAI\Client('YOUR_OPENAI_API_KEY');

// creates an embedding vector representing the input text
var_dump($client->createEmbedding(model: 'text-embedding-ada-002', input: 'The food was delicious and the waiter...'));
```

### Audio

[](#audio)

#### Create transcription

[](#create-transcription)

Transcribes audio into the input language. See the [OpenAI docs](https://platform.openai.com/docs/api-reference/audio/create) for more information.

```
// initialize the client
$client = new OpenAI\Client('YOUR_OPENAI_API_KEY');

// transcribes audio into the input language
var_dump($client->createTranscription(file: 'audio.mp3', model: 'whisper-1'));
```

#### Create translation

[](#create-translation)

Translates audio into English. See the [OpenAI docs](https://platform.openai.com/docs/api-reference/audio/create) for more information.

```
// initialize the client
$client = new OpenAI\Client('YOUR_OPENAI_API_KEY');

// translates audio into english
var_dump($client->createTranslation(file: 'german.m4a', model: 'whisper-1'));
```

### Files

[](#files)

#### List files

[](#list-files)

Returns a list of files that belong to the user's organization. See the [OpenAI docs](https://platform.openai.com/docs/api-reference/files/list) for more information.

```
// initialize the client
$client = new OpenAI\Client('YOUR_OPENAI_API_KEY');

// returns a list of files that belong to the user's organization
var_dump($client->listFiles());
```

#### Upload file

[](#upload-file)

Upload a file that contains document(s) to be used across various endpoints/features. Currently, the size of all the files uploaded by one organization can be up to 1 GB. Please contact OpenAI if you need to increase the storage limit. See the [OpenAI docs](https://platform.openai.com/docs/api-reference/files/upload) for more information.

```
// initialize the client
$client = new OpenAI\Client('YOUR_OPENAI_API_KEY');

// upload a file that contains document(s) to be used across various endpoints/features
var_dump($client->createFile(file: 'mydata.jsonl', purpose: 'fine-tune'));
```

#### Delete file

[](#delete-file)

Delete a file. See the [OpenAI docs](https://platform.openai.com/docs/api-reference/files/delete) for more information.

```
// initialize the client
$client = new OpenAI\Client('YOUR_OPENAI_API_KEY');

// delete a file
var_dump($client->deleteFile(file_id: 'file-XjGxS3KTG0uNmNOK362iJua3'));
```

#### Retrieve file

[](#retrieve-file)

Returns information about a specific file. See the [OpenAI docs](https://platform.openai.com/docs/api-reference/files/retrieve) for more information.

```
// initialize the client
$client = new OpenAI\Client('YOUR_OPENAI_API_KEY');

// returns information about a specific file
var_dump($client->retrieveFile(file_id: 'file-XjGxS3KTG0uNmNOK362iJua3'));
```

#### Retrieve file content

[](#retrieve-file-content)

Returns the contents of the specified file. See the [OpenAI docs](https://platform.openai.com/docs/api-reference/files/retrieve-content) for more information.

```
// initialize the client
$client = new OpenAI\Client('YOUR_OPENAI_API_KEY');

// returns the contents of the specified file
var_dump($client->downloadFile(file_id: 'file-XjGxS3KTG0uNmNOK362iJua3'));
```

### Fine-Tunes

[](#fine-tunes)

#### Create fine-tune

[](#create-fine-tune)

Creates a job that fine-tunes a specified model from a given dataset.

Response includes details of the enqueued job including job status and the name of the fine-tuned models once complete.

See the [OpenAI docs](https://platform.openai.com/docs/api-reference/fine-tunes/create) for more information.

```
// initialize the client
$client = new OpenAI\Client('YOUR_OPENAI_API_KEY');

// creates a job that fine-tunes a specified model from a given dataset
var_dump($client->createFineTune(training_file: 'file-XGinujblHPwGLSztz8cPS8XY'));
```

#### List fine-tunes

[](#list-fine-tunes)

List your organization's fine-tuning jobs. See the [OpenAI docs](https://platform.openai.com/docs/api-reference/fine-tunes/list) for more information.

```
// initialize the client
$client = new OpenAI\Client('YOUR_OPENAI_API_KEY');

// list your organization's fine-tuning jobs

var_dump($client->listFineTunes());
```

#### Retrieve fine-tune

[](#retrieve-fine-tune)

Gets info about the fine-tune job. See the [OpenAI docs](https://platform.openai.com/docs/api-reference/fine-tunes/retrieve) for more information.

```
// initialize the client
$client = new OpenAI\Client('YOUR_OPENAI_API_KEY');

// gets info about the fine-tune job
var_dump($client->retrieveFineTune(fine_tune_id: 'ft-AF1WoRqd3aJAHsqc9NY7iL8F'));
```

#### Cancel fine-tune

[](#cancel-fine-tune)

Immediately cancel a fine-tune job. See the [OpenAI docs](https://platform.openai.com/docs/api-reference/fine-tunes/cancel) for more information.

```
// initialize the client
$client = new OpenAI\Client('YOUR_OPENAI_API_KEY');

// immediately cancel a fine-tune job
var_dump($client->cancelFineTune(fine_tune_id: 'ft-AF1WoRqd3aJAHsqc9NY7iL8F'));
```

#### List fine-tune events

[](#list-fine-tune-events)

Get fine-grained status updates for a fine-tune job. See the [OpenAI docs](https://platform.openai.com/docs/api-reference/fine-tunes/events) for more information.

```
// initialize the client
$client = new OpenAI\Client('YOUR_OPENAI_API_KEY');

// get fine-grained status updates for a fine-tune job.

var_dump($client->listFineTuneEvents(fine_tune_id: 'ft-AF1WoRqd3aJAHsqc9NY7iL8F'));
```

#### Delete fine-tune model

[](#delete-fine-tune-model)

Delete a fine-tuned model. You must have the Owner role in your organization. See the [OpenAI docs](https://platform.openai.com/docs/api-reference/fine-tunes/delete-model) for more information.

```
// initialize the client
$client = new OpenAI\Client('YOUR_OPENAI_API_KEY');

// delete a fine-tuned model
var_dump($client->deleteModel(model: 'curie:ft-acmeco-2021-03-03-21-44-20'));
```

### Moderations

[](#moderations)

#### Create moderation

[](#create-moderation)

Classifies if text violates OpenAI's Content Policy. See the [OpenAI docs](https://platform.openai.com/docs/api-reference/moderations/create) for more information.

```
// initialize the client
$client = new OpenAI\Client('YOUR_OPENAI_API_KEY');

// classifies if text violates openai's content policy
var_dump($client->createModeration(input: 'I want to kill them.'));
```

###  Health Score

37

—

LowBetter than 81% of packages

Maintenance56

Moderate activity, may be stable

Popularity26

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity46

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~3 days

Total

7

Last Release

1215d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/8904453?v=4)[Steven van de Graaf](/maintainers/sgraaf)[@sgraaf](https://github.com/sgraaf)

---

Top Contributors

[![sgraaf](https://avatars.githubusercontent.com/u/8904453?v=4)](https://github.com/sgraaf "sgraaf (14 commits)")

---

Tags

phpapiclientsdkGPT-3openaiChatGpt

###  Code Quality

Static AnalysisPHPStan

Code StylePHP CS Fixer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/sgraaf-openai-php/health.svg)

```
[![Health](https://phpackages.com/badges/sgraaf-openai-php/health.svg)](https://phpackages.com/packages/sgraaf-openai-php)
```

###  Alternatives

[openai-php/client

OpenAI PHP is a supercharged PHP API client that allows you to interact with the Open AI API

5.8k28.0M318](/packages/openai-php-client)[openai-php/laravel

OpenAI PHP for Laravel is a supercharged PHP API client that allows you to interact with the Open AI API

3.7k9.5M89](/packages/openai-php-laravel)[openai-php/symfony

Symfony Bundle for OpenAI

220871.3k3](/packages/openai-php-symfony)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
