Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
C

client

> AI 编程
Open source

⚡️ OpenAI PHP is a supercharged community-maintained PHP API client that allows you to interact with OpenAI API.

5.8K stars0 likes6 views
WebsiteGitHub

About

⚡️ OpenAI PHP is a supercharged community-maintained PHP API client that allows you to interact with OpenAI API.

<p align="center">
    <a href="https://github.com/openai-php/client/actions"></a>
    <a href="https://packagist.org/packages/openai-php/client"></a>
    <a href="https://packagist.org/packages/openai-php/client"></a>
    <a href="https://packagist.org/packages/openai-php/client"></a>
</p>


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:

bash
composer require openai-php/client

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.

bash
composer require guzzlehttp/guzzle

Then, interact with OpenAI's API:

php
$yourApiKey = getenv('YOUR_API_KEY');
$client = OpenAI::client($yourApiKey);

$response = $client->responses()->create([
    'model' => 'gpt-4o',
    'input' => 'Hello!',
]);

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

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.

php
$response = $client->models()->list();

$response->object; // 'list'

foreach ($response->data as $result) {
    $result->id; // 'gpt-3.5-turbo-instruct'
    $result->object; // 'model'
    // ...
}

$response->toArray(); // ['object' => 'list', 'data' => [...]]

retrieve

Retrieves a model instance, providing basic information about the model such as the owner and permissioning.

php
$response = $client->models()->retrieve('gpt-3.5-turbo-instruct');

$response->id; // 'gpt-3.5-turbo-instruct'
$response->object; // 'model'
$response->created; // 1642018370
$response->ownedBy; // 'openai'

$response->toArray(); // ['id' => 'gpt-3.5-turbo-instruct', ...]

delete

Delete a fine-tuned model.

php
$response = $client->models()->delete('curie:ft-acmeco-2021-03-03-21-44-20');

$response->id; // 'curie:ft-acmeco-2021-03-03-21-44-20'
$response->object; // 'model'
$response->deleted; // true

$response->toArray(); // ['id' => 'curie:ft-acmeco-2021-03-03-21-44-20', ...]

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.

php
$stream = $client->responses()->createStreamed([
    'model' => 'gpt-4o-mini',
    'tools' => [
        [
            'type' => 'web_search_preview'
        ]
    ],
    'input' => "what was a positive news story from today?",
]);

foreach ($stream as $response) {
    $response->event; // 'response.created'
}

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.

php
$stream = $client->responses()->retrieveStreamed('resp_67ccd2bed1ec8190b14f964abc054267', [
    'starting_after' => '2',
]);

foreach ($stream as $response) {
    $response->event; // 'response.created'
}

cancel

Cancel a model response (background request) with the given ID.

php
$response = $client->responses()->cancel('resp_67ccd2bed1ec8190b14f964abc054267');

$response->id; // 'resp_67ccd2bed1ec8190b14f964abc054267'
$response->status; // 'canceled'

$response->toArray(); // ['id' => 'resp_67ccd2bed1ec8190b14f964abc054267', 'status' => 'canceled', ...]

delete

Deletes a model response with the given ID.

php
$response = $client->responses()->delete('resp_67ccd2bed1ec8190b14f964abc054267');

$response->id; // 'resp_67ccd2bed1ec8190b14f964abc054267'
$response->object; // 'response'
$response->deleted; // true

$response->toArray(); // ['id' => 'resp_67ccd2bed1ec8190b14f964abc054267', 'deleted' => true, ...]

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.

php
$response = $client->conversations()->create([
    'metadata' => ['topic' => 'demo'],
    'items' => [
        [
            'type' => 'message',
            'role' => 'user',
            'content' => 'Hello!'
        ],
    ],
]);

$response->id; // 'conv_123'
$response->object; // 'conversation'
$response->createdAt; // 1741900000
$response->metadata; // ['topic' => 'demo']

$response->toArray(); // ['id' => 'conv_123', 'object' => 'conversation', ...]

retrieve

Retrieve a conversation by ID.

php
$response = $client->conversations()->retrieve('conv_123');

$response->id; // 'conv_123'
$response->object; // 'conversation'
$response->createdAt; // 1741900000

$response->toArray(); // ['id' => 'conv_123', 'object' => 'conversation', ...]

update

Update a conversation by ID.

php
$response = $client->conversations()->update('conv_123', [
    'metadata' => ['foo' => 'bar'],
]);

$response->id; // 'conv_123'
$response->metadata; // ['foo' => 'bar']

delete

Delete a conversation by ID.

php
$response = $client->conversations()->delete('conv_123');

$response->id; // 'conv_123'
$response->object; // 'conversation.deleted'
$response->deleted; // true

$response->toArray(); // ['id' => 'conv_123', 'object' => 'conversation.deleted', 'deleted' => true]

Conversations Items Resource

create

Create items for a conversation.

php
$response = $client->conversations()->items()->create('conv_123', [
    'items' => [
        [
            'role' => 'system',
            'content' => 'Refer to me as PHPBot.',
        ],
    ],
]);

foreach ($response->data as $listItem) {
    $listItem->item; // The created item (e.g., message)
}

$response->firstId; // 'msg_abc'
$response->lastId; // 'msg_abc'
$response->hasMore; // false

$response->toArray(); // ['object' => 'list', 'data' => [...], ...]

list

List items for a conversation.

php
$response = $client->conversations()->items()->list('conv_123', [
    'limit' => 10,
]);

$response->object; // 'list'

retrieve

Retrieve a specific item from a conversation.

php
$response = $client->conversations()->items()->retrieve('conv_123', 'msg_abc', [
    'include' => ['step_details'],
]);

$response->id; // 'msg_abc'
$response->type; // 'message'
$response->status; // 'completed'

delete

Delete a specific item from a conversation. Returns the updated conversation.

php
$response = $client->conversations()->items()->delete('conv_123', 'msg_abc');

$response->id; // 'conv_123'
$response->object; // 'conversation'

Containers Resource

create

Creates a container for use with the Code Interpreter tool.

php
$response = $client->containers()->create([
    'name' => 'My Container',
    'expires_after' => [
        'anchor' => 'last_active_at',
        'minutes' => 60,
    ],
]);

$response->id; // 'container_abc123'
$response->object; // 'container'
$response->createdAt; // 1690000000
$response->status; // 'active'
$response->expiresAfter->anchor; // 'last_active_at'
$response->expiresAfter->minutes; // 60
$response->lastActiveAt; // 1690001000
$response->name; // 'My Container'

$response->toArray(); // ['id' => 'container_abc123', 'object' => 'container', ...]

list

Returns a list of containers.

…

retrieve

Retrieves a container with the given ID.

php
$response = $client->containers()->retrieve('container_abc123');

$response->id; // 'container_abc123'
$response->object; // 'container'
$response->createdAt; // 1690000000
$response->status; // 'active'
$response->expiresAfter->anchor; // 'last_active_at'
$response->expiresAfter->minutes; // 60
$response->lastActiveAt; // 1690001000
$response->name; // 'Test Container'

$response->toArray(); // ['id' => 'container_abc123', 'object' => 'container', ...]

delete

Delete a container with the given ID.

php
$response = $client->containers()->delete('container_abc123');

$response->id; // 'container_abc123'
$response->object; // 'container'
$response->deleted; // true

$response->toArray(); // ['id' => 'container_abc123', 'object' => 'container', 'delet

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

PHPapiclientcodexgpt-3

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 17, 2026
CategoryAI 编程
PricingOpen source

> Related tools

G
GitHub Copilot
GitHub 官方 AI 编程助手,覆盖补全、Chat 与 Agent 模式。
C
Cursor
AI 原生代码编辑器,对话改代码、多文件 Agent 与规则体系是其核心。
S
skills
Skills for Real Engineers. Straight from my .agents directory.