Download the PHP package openai-php/client without Composer
On this page you can find all versions of the php package openai-php/client. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package client
OpenAI PHP is a community-maintained PHP API client that allows you to interact with the Open AI API.
- Follow the creator Nuno Maduro:
- YouTube: youtube.com/@nunomaduro — Videos every weekday
- Twitch: twitch.tv/enunomaduro — Streams (almost) every weekday
- Twitter / X: x.com/enunomaduro
- LinkedIn: linkedin.com/in/nunomaduro
- Instagram: instagram.com/enunomaduro
- Tiktok: tiktok.com/@enunomaduro
If you or your business relies on this package, it's important to support the developers who have contributed their time and effort to create and maintain this valuable tool:
- Nuno Maduro: github.com/sponsors/nunomaduro
- Sandro Gehri: github.com/sponsors/gehrisandro
- Connor Tumbleson: github.com/sponsors/iBotPeaches
Table of Contents
- Get Started
- Usage
- Models Resource
- Responses Resource
- Conversations Resource
- Conversations Items Resource
- Containers Resource
- Containers Files Resource
- Chat Resource
- Audio Resource
- Embeddings Resource
- Files Resource
- FineTuning Resource
- Moderations Resource
- Images Resource
- Vector Stores Resource
- Vector Stores Files Resource
- Vector Stores File Batches Resource
- Batches Resource
- Realtime Ephemeral Keys
- Completions Resource (legacy)
- Assistants Resource (deprecated)
- Thread Resource (deprecated)
- Thread Messages Resource (deprecated)
- Thread Runs Resource (deprecated)
- Thread Runs Steps Resource (deprecated)
- FineTunes Resource (deprecated)
- Edits Resource (deprecated)
- Meta Information
- Troubleshooting
- Testing
- [Webhooks][#webhooks]
- Services
- Azure
Get Started
Requires PHP 8.2+
First, install OpenAI via the Composer package manager:
Ensure that the php-http/discovery composer plugin is allowed to run or install a client manually if your project does not already have a PSR-18 client integrated.
Then, interact with OpenAI's API:
If necessary, it is possible to configure and create a separate client.
Usage
Models Resource
list
Lists the currently available models, and provides basic information about each one such as the owner and availability.
retrieve
Retrieves a model instance, providing basic information about the model such as the owner and permissioning.
delete
Delete a fine-tuned model.
Responses Resource
create
Creates a model response. Provide text or image inputs to generate text or JSON outputs. Have the model call your own custom code or use built-in tools like web search or file search to use your own data as input for the model's response.
Create a model response with a function tool.
create streamed
When you create a Response with stream set to true, the server will emit server-sent events to the client as the Response is generated. All events and their payloads can be found in OpenAI docs.
retrieve
Retrieves a model response with the given ID.
retrieve streamed
When you retrieve a Response with stream set to true, the server will emit server-sent events to the client as the Response is generated. All events and their payloads can be found in OpenAI docs.
cancel
Cancel a model response (background request) with the given ID.
delete
Deletes a model response with the given ID.
list
Lists input items for a response with the given ID. All events and their payloads can be found in OpenAI docs.
Conversations Resource
create
Create a conversation.
retrieve
Retrieve a conversation by ID.
update
Update a conversation by ID.
delete
Delete a conversation by ID.
Conversations Items Resource
create
Create items for a conversation.
list
List items for a conversation.
retrieve
Retrieve a specific item from a conversation.
delete
Delete a specific item from a conversation. Returns the updated conversation.
Containers Resource
create
Creates a container for use with the Code Interpreter tool.
list
Returns a list of containers.
retrieve
Retrieves a container with the given ID.
delete
Delete a container with the given ID.
Containers Files Resource
create
Create or upload a file into a container.
[!NOTE] You must provide either
fileorfile_id, but not both.
list
Returns a list of files in the container.
retrieve
Retrieve information about a container file.
retrieve content
Returns the raw content of the specified container file.
delete
Delete a container file.
Chat Resource
create
Creates a completion for the chat message.
Creates a completion for the chat message with a tool call.
Creates a completion for the chat message with a function call.
Creates a chat completion with image input via image_url.
Useful for describing and analyzing visual content.
create streamed
Creates a streamed completion for the chat message.
To get usage report when using stream you can use include_usage in stream_options .
usage is always null except for the last chunk which contains the token usage statistics for the entire request.
Audio Resource
speech
Generates audio from the input text.
speechStreamed
Generates streamed audio from the input text.
transcribe
Transcribes audio into the input language.
transcribe streamed
Transcribes audio into the input language with streaming.
translate
Translates audio into English.
Embeddings Resource
create
Creates an embedding vector representing the input text.
Files Resource
list
Returns a list of files that belong to the user's organization.
delete
Delete a file.
retrieve
Returns information about a specific file.
upload
Upload a file that contains document(s) to be used across various endpoints/features.
download
Returns the contents of the specified file.
FineTuning Resource
create job
Creates a job that fine-tunes a specified model from a given dataset.
list jobs
List your organization's fine-tuning jobs.
You can pass additional parameters to the listJobs method to narrow down the results.
retrieve job
Get info about a fine-tuning job.
cancel job
Immediately cancel a fine-tune job.
list job events
Get status updates for a fine-tuning job.
You can pass additional parameters to the listJobEvents method to narrow down the results.
Moderations Resource
create
Classifies if text violates OpenAI's Content Policy.
Images Resource
create
Creates an image given a prompt.
create streamed
When you create an image with stream set to true, the server will emit server-sent events to the client as the image is generated. All events and their payloads can be found in OpenAI docs.
edit
Creates an edited or extended image given an original image and a prompt.
edit streamed
When you edit an image with stream set to true, the server will emit server-sent events to the client as the image is generated. All events and their payloads can be found in OpenAI docs.
variation
Creates a variation of a given image.
Batches Resource
create
Creates a batch.
retrieve
Retrieves a batch.
cancel
Cancels a batch.
list
Returns a list of batches.
Vector Stores Resource
create
Create a vector store.
retrieve
Retrieves a vector store.
modify
Modifies a vector store.
delete
Delete a vector store.
list
Returns a list of vector stores.
Vector Store Files Resource
create
Create a vector store file by attaching a File to a vector store.
retrieve
Retrieves a vector store file.
delete
Delete a vector store file. This will remove the file from the vector store but the file itself will not be deleted. To delete the file, use the delete file endpoint.
list
Returns a list of vector store files.
search
Search a vector store for relevant chunks based on a query and file attributes filter.
Vector Store File Batches Resource
create
Create a vector store file batch.
retrieve
Retrieves a vector store file batch.
cancel
Cancel a vector store file batch. This attempts to cancel the processing of files in this batch as soon as possible.
list
Returns a list of vector store files.
Realtime Ephemeral Keys
token
Create an ephemeral API token for real time sessions.
transcribeToken
Create an ephemeral API token for real time transcription sessions.
Completions Resource (legacy)
[!WARNING]
TheCompletionsresource was marked "Legacy" by OpenAI in July 2023. Please use theChatresource instead.
Completion API Information
#### `create` Creates a completion for the provided prompt and parameters. #### `create streamed` Creates a streamed completion for the provided prompt and parameters.Assistants Resource (deprecated)
[!WARNING] OpenAI has deprecated the Assistants API and will stop working by August 26, 2026. https://platform.openai.com/docs/guides/migrate-to-responses#assistants-api
Assistants API Information
> **Note** - If you are creating the client manually from the factory. Make sure you provide the necessary header: > #### `create` Create an assistant with a model and instructions. #### `retrieve` Retrieves an assistant. #### `modify` Modifies an assistant. #### `delete` Delete an assistant. #### `list` Returns a list of assistants.Threads Resource (deprecated)
[!WARNING] OpenAI has deprecated the Assistants API and will stop working by August 26, 2026. https://platform.openai.com/docs/guides/migrate-to-responses#assistants-api
Threads API Information
#### `create` Create a thread. #### `createAndRun` Create a thread and run it in one request. #### `retrieve` Retrieves a thread. #### `modify` Modifies a thread. #### `delete` Delete a thread.Thread Messages Resource (deprecated)
[!WARNING] OpenAI has deprecated the Assistants API and will stop working by August 26, 2026. https://platform.openai.com/docs/guides/migrate-to-responses#assistants-api
Thread Messages API Information
#### `create` Create a message. #### `retrieve` Retrieve a message. #### `modify` Modifies a message. #### `delete` Deletes a message. #### `list` Returns a list of messages for a given thread.Thread Runs Resource (deprecated)
[!WARNING] OpenAI has deprecated the Assistants API and will stop working by August 26, 2026. https://platform.openai.com/docs/guides/migrate-to-responses#assistants-api
Thread Runs API Information
#### `create` Create a run. #### `create streamed` Creates a streamed run. [OpenAI Assistant Events](https://platform.openai.com/docs/api-reference/assistants-streaming/events) #### `create streamed with function calls` Creates a streamed run with function calls [OpenAI Assistant Events](https://platform.openai.com/docs/api-reference/assistants-streaming/events) #### `retrieve` Retrieves a run. #### `modify` Modifies a run. #### `cancel` Cancels a run that is `in_progress`. #### `submitToolOutputs` When a run has the status: `requires_action` and `required_action.type` is `submit_tool_outputs`, this endpoint can be used to submit the outputs from the tool calls once they're all completed. All outputs must be submitted in a single request. #### `list` Returns a list of runs belonging to a thread.Thread Run Steps Resource (deprecated)
[!WARNING] OpenAI has deprecated the Assistants API and will stop working by August 26, 2026. https://platform.openai.com/docs/guides/migrate-to-responses#assistants-api
Thread Run Steps API Information
#### `retrieve` Retrieves a run step. #### `list` Returns a list of run steps belonging to a run.Edits Resource (deprecated)
[!WARNING] OpenAI has deprecated the Edits API and will stop working by January 4, 2024. https://openai.com/blog/gpt-4-api-general-availability#deprecation-of-the-edits-api
Edits API Information
#### `create` Creates a new edit for the provided input, instruction, and parameters.FineTunes Resource (deprecated)
[!WARNING] OpenAI has deprecated the FineTunes API and will stop working by January 4, 2024 https://platform.openai.com/docs/deprecations#2023-08-22-fine-tunes-endpoint
FineTunes API Information
#### `create` Creates a job that fine-tunes a specified model from a given dataset. #### `list` List your organization's fine-tuning jobs. #### `retrieve` Gets info about the fine-tune job. #### `cancel` Immediately cancel a fine-tune job. #### `list events` Get fine-grained status updates for a fine-tune job. #### `list events streamed` Get streamed fine-grained status updates for a fine-tune job.Meta Information
On all response objects you can access the meta information returned by the API via the meta() method.
The toArray() method returns the meta information in the form originally returned by the API.
On streaming responses you can access the meta information on the response stream object.
For further details about the rates limits and what to do if you hit them visit the OpenAI documentation.
Troubleshooting
Timeout
You may run into a timeout when sending requests to the API. The default timeout depends on the HTTP client used.
You can increase the timeout by configuring the HTTP client and passing in to the factory.
This example illustrates how to increase the timeout using Guzzle.
Testing
The package provides a fake implementation of the OpenAI\Client class that allows you to fake the API responses.
To test your code ensure you swap the OpenAI\Client class with the OpenAI\Testing\ClientFake class in your test case.
The fake responses are returned in the order they are provided while creating the fake client.
All responses are having a fake() method that allows you to easily create a response object by only providing the parameters relevant for your test case.
In case of a streamed response you can optionally provide a resource holding the fake response data.
After the requests have been sent there are various methods to ensure that the expected requests were sent:
To write tests expecting the API request to fail you can provide a Throwable object as the response.
Webhooks
The package includes a signature verifier for OpenAI webhooks. To verify the signature of incoming webhook requests, you can use the OpenAI\Webhooks\SignatureVerifier class.
Services
Azure
In order to use the Azure OpenAI Service, it is necessary to construct the client manually using the factory.
To use Azure, you must deploy a model, identified by the {deployment-id}, which is already incorporated into the API calls. As a result, you do not have to provide the model during the calls since it is included in the BaseUri.
Therefore, a basic sample completion call would be:
OpenAI PHP is an open-sourced software licensed under the MIT license.
All versions of client with dependencies
php-http/discovery Version ^1.20.0
php-http/multipart-stream-builder Version ^1.4.2
psr/http-client Version ^1.0.3
psr/http-client-implementation Version ^1.0.1
psr/http-factory-implementation Version *
psr/http-message Version ^1.1.0|^2.0.0