OpenAI SDK for PHP


December 29th, 2021

OpenAI SDK for PHP

The orhanerday/open-ai package is a PHP SDK for accessing the OpenAI GPT-3 API. It supports the complete, search, answer, classification, and engine APIs.

Here's an example of using the OpenAI classification API with this package:

$response = $open_ai->classification([
"examples" => [
["A happy moment", "Positive"],
["I am sad.", "Negative"],
["I am feeling awesome", "Positive"]
"labels" => ["Positive", "Negative", "Neutral"],
"query" => "It is a rainy day :(",
"search_model" => "ada",
"model" => "curie",

You'll get back the following classification for the "It is a rainy day :(" query:

"completion": "cmpl-4KKvYbROfgIroNbeTBPmAmAzKZcUC",
"label": "Negative",
"model": "curie:2020-05-03",
"object": "classification",
"search_model": "ada",
"selected_examples": [
"document": 1,
"label": "Negative",
"text": "I am sad."
"document": 0,
"label": "Positive",
"text": "A happy moment"
"document": 2,
"label": "Positive",
"text": "I am feeling awesome"

This package is lightweight, with no external dependencies apart from the cURL and JSON PHP extensions. You'll be required to decode the JSON API responses to work with the data:

$response = $open_ai->classification([/* ... */]);
$data = json_decode($response, true);

To get started with this package, you'll first want to be familiar with the OpenAI API documentation and examples.

You can learn more about this package, get full installation instructions, and view the source code on GitHub.

Filed in:

Paul Redmond

Full stack web developer. Author of Lumen Programming Guide and Docker for PHP Developers.