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

public-openai-client-php

> DevOps
Open source

OpenAI API Client for PHP. Includes all endpoints and models (DTOs) for all requests and responses.

336 stars0 likes0 views
WebsiteGitHub

About

OpenAI API Client for PHP. Includes all endpoints and models (DTOs) for all requests and responses.

Tectalic OpenAI REST API Client

This package is no longer supported or maintained.

Introduction

The Tectalic OpenAI REST API Client is a package that provides a convenient and straightforward way to interact with the OpenAI API from your PHP application.

Supports ChatGPT, GPT-4, GPT-3.5, GPT-3, Codex, DALL·E, Whisper, Fine-Tuning, Embeddings and Moderation models, with fully typed Data Transfer Objects (DTOs) for all requests and responses and IDE autocomplete support.

More information is available from https://tectalic.com/apis/openai.

This is an unofficial package and has no affiliations with OpenAI.

Examples

Integrating OpenAI into your application is now as simple as a few lines of code.

Chat Completion using ChatGPT (GPT-3.5 & GPT-4)

…

Learn more about chat completion.

This handler supports both the GPT-3.5 and GPT-4 models:

GPT-3.5

Supported GPT-3.5 models include gpt-3.5-turbo and more.

GPT-4

Supported GPT-4 models include gpt-4 and more.

Note: GPT-4 is currently in a limited beta and is only accessible to those who have been granted access. Please see here for details and instructions on how to join the waitlist.

If you receive a 404 error when attempting to use GPT-4, then your OpenAI account has not been granted access.

Chat Completion Function Calling using ChatGPT (GPT-3.5 & GPT-4)

The following example uses the gpt-3.5-turbo-0613 model to demonstrate function calling.

It converts natural language into a function call, which can then be executed within your application.

…

Learn more about function calling.

Text Completion (GPT-3)

$openaiClient = \Tectalic\OpenAi\Manager::build(new \GuzzleHttp\Client(), new \Tectalic\OpenAi\Authentication(getenv('OPENAI_API_KEY')));

/** @var \Tectalic\OpenAi\Models\Completions\CreateResponse $response */
$response = $openaiClient->completions()->create(
    new \Tectalic\OpenAi\Models\Completions\CreateRequest([
        'model'  => 'text-davinci-003',
        'prompt' => 'Will using a third party package save time?',
    ])
)->toModel();

echo $response->choices[0]->text;
// Using a third party package can save time because you don't have to write the code yourself.

This handler supports all GPT-3 models, including text-davinci-003, text-davinci-002 and more.

Learn more about text completion.

Code Completion (Codex)

$openaiClient = \Tectalic\OpenAi\Manager::build(new \GuzzleHttp\Client(), new \Tectalic\OpenAi\Authentication(getenv('OPENAI_API_KEY')));

/** @var \Tectalic\OpenAi\Models\Completions\CreateResponse $response */
$response = $openaiClient->completions()->create(
    new \Tectalic\OpenAi\Models\Completions\CreateRequest([
        'model'  => 'code-davinci-002',
        'prompt' => "// PHP 8\n// A variable that saves the current date and time",
        'max_tokens' => 256,
        'stop' => ";",
    ])
)->toModel();

echo $response->choices[0]->text;
// $now = date("Y-m-d G:i:s")

Supported Codex models include code-davinci-002 and code-cushman-001.

Learn more about code completion.

Image Generation (DALL·E)

$openaiClient = \Tectalic\OpenAi\Manager::build(new \GuzzleHttp\Client(), new \Tectalic\OpenAi\Authentication(getenv('OPENAI_API_KEY')));

/** @var \Tectalic\OpenAi\Models\ImagesGenerations\CreateResponse $response */
$response = $openaiClient->imagesGenerations()->create(
    new \Tectalic\OpenAi\Models\ImagesGenerations\CreateRequest([
        'prompt' => 'A cute baby sea otter wearing a hat',
        'size' => '256x256',
        'n' => 5
    ])
)->toModel();

foreach ($response->data as $item) {
    var_dump($item->url);
}

Learn more about image generation.

Speech to Text Audio Transcription (Whisper)

$openaiClient = \Tectalic\OpenAi\Manager::build(new \GuzzleHttp\Client(), new \Tectalic\OpenAi\Authentication(getenv('OPENAI_API_KEY')));

/** @var \Tectalic\OpenAi\Models\AudioTranscriptions\CreateResponse $response */
$response = $openaiClient->audioTranscriptions()->create(
    new \Tectalic\OpenAi\Models\AudioTranscriptions\CreateRequest([
        'file' => '/full/path/to/audio/file.mp3',
        'model' => 'whisper-1',
    ])
)->toModel();

echo $response->text;
// Your audio transcript in your source language...

Supported Whisper models include whisper-1.

Learn more about speech to text, including the 50+ supported languages.

Speech to Text Audio Translation (Whisper)

$openaiClient = \Tectalic\OpenAi\Manager::build(new \GuzzleHttp\Client(), new \Tectalic\OpenAi\Authentication(getenv('OPENAI_API_KEY')));

/** @var \Tectalic\OpenAi\Models\AudioTranslations\CreateResponse $response */
$response = $openaiClient->audioTranslations()->create(
    new \Tectalic\OpenAi\Models\AudioTranslations\CreateRequest([
        'file' => '/full/path/to/audio/file.mp3',
        'model' => 'whisper-1',
    ])
)->toModel();

echo $response->text;
// Your audio transcript in English...

Supported Whisper models include whisper-1.

Learn more about speech to text, including the 50+ supported languages.

Installation

Need help getting started? See our guide: how to build an app using the OpenAI API.

System Requirements

  • PHP version 7.2.5 or newer (including PHP 8.0 and 8.1)
  • PHP JSON extension installed if using PHP 7.x. As of PHP 8.0, this extension became a core PHP extension so is always enabled.
  • A PSR-18 compatible HTTP client such as 'Guzzle' or the 'Symfony HTTP Client'.

Composer Installation

Install the package into your project:

composer require tectalic/openai

Usage

After installing the Tectalic OpenAI REST API Client package into your project, ensure you also have a compatible PSR-18 HTTP client such as 'Guzzle' or the Symfony 'HTTP Client'.

You can use the following code sample and customize it to suit your application.

// Load your project's composer autoloader (if you aren't already doing so).
require_once(__DIR__ . '/vendor/autoload.php');
use Symfony\Component\HttpClient\Psr18Client;
use Tectalic\OpenAi\Authentication;
use Tectalic\OpenAi\Client;
use Tectalic\OpenAi\Manager;

// Build a Tectalic OpenAI REST API Client globally.
$auth = new Authentication(getenv('OPENAI_API_KEY'));
$httpClient = new Psr18Client();
Manager::build($httpClient, $auth);

// or

// Build a Tectalic OpenAI REST API Client manually.
$auth = new Authentication(getenv('OPENAI_API_KEY'));
$httpClient = new Psr18Client();
$client = new Client($httpClient, $auth, Manager::BASE_URI);

Authentication

To authenticate your API requests, you will need to provide an Authentication ($auth) object when calling Manager::build().

Authentication to the OpenAI API is by HTTP Bearer authentication.

Please see the OpenAI API documentation for more details on obtaining your authentication credentials.

In the Usage code above, customize the Authentication constructor to your needs. For example, will likely need to add a OPENAI_API_KEY environment variable to your system.

Client Class

The primary class you will interact with is the Client class (Tectalic\OpenAi\Client).

This Client class also contains the helper methods that let you quickly access the 19 API Handlers.

Please see below for a complete list of supported handlers and methods.

Supported API Handlers and Methods

This package supports 28 API Methods, which are grouped into 19 API Handlers.

See the table below for a full list of API Handlers and Methods.

API Handler Class and Method Name Description API Verb and URL
AudioTranscriptions::create() Transcribes audio into the input language. POST /audio/transcriptions
AudioTranslations::create() Translates audio into English. POST /audio/translations
ChatCompletions::create() Creates a model response for the given chat conversation. POST /chat/completions
Completions::create() Creates a completion for the provided prompt and parameters. POST /completions
Edits::create() Creates a new edit for the provided input, instruction, and parameters. POST /edits
Embeddings::create() Creates an embedding vector representing the input text. POST /embeddings
Files::list() Returns a list of files that belong to the user's organization. GET /files
Files::create() 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 us if you need to increase the storage limit. POST /files
Files::retrieve() Returns information about a specific file. GET /files/{file_id}
Files::delete() Delete a file. DELETE /files/{file_id}
FilesContent::download() Returns the contents of the specified file. GET /files/{file_id}/content
FineTunes::list() List your organization's fine-tuning jobs GET /fine-tunes
FineTunes::create() ~~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.
Learn more about fine-tuning~~ POST /fine-tunes
FineTunes::retrieve() ~~Gets info about the fine-tune job.
Learn more about fine-tuning~~ GET /fine-tunes/{fine_tune_id}
FineTunesCancel::cancelFineTune() Immediately cancel a fine-tune job. POST /fine-tunes/{fine_tune_id}/cancel
FineTunesEvents::listFineTune() Get fine-grained status updates for a fine-tune job. GET /fine-tunes/{fine_tune_id}/events
FineTuningJobs::listPaginated() List your organization's fine-tuning jobs GET /fine_tuning/jobs
FineTuningJobs::create() 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.
Learn more about fine-tuning POST /fine_tuning/jobs
FineTuningJobs::retrieve() Get info about a fine-tuning job.
Learn more about fine-tuning GET /fine_tuning/jobs/{fine_tuning_job_id}
FineTuningJobsCancel::fineTuning() Immediately cancel a fine-tune job. POST /fine_tuning/jobs/{fine_tuning_job_id}/cancel
FineTuningJobsEvents::listFineTuning() Get status updates for a fine-tuning job. GET /fine_tuning/jobs/{fine_tuning_job_id}/events
ImagesEdits::createImage() Creates an edited or extended image given an original image and a prompt. POST /images/edits
ImagesGenerations::create() Creates an image given a prompt. POST /images/generations
ImagesVariations::createImage() Creates a

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

PHPapiapi-clientapi-client-phpartificial-intelligence

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 18, 2026
CategoryDevOps
PricingOpen source

> Related tools

D
Docker
容器化平台,标准化应用交付
G
GitHub Actions
GitHub 原生 CI/CD 工作流
N
Nginx
高性能 Web 服务器与反向代理