The OpenAI PHP is an extension that allows PHP developers to integrate the powerful capabilities of OpenAI into their applications. Activating the extension on the web server will allow users to quickly access the services offered by OpenAI without additional libraries in PHP.
OpenAI PHP uses the PHP-CPP, OpenAI C++ and JSON for Modern C++ libraries.
Download the latest release of the library for Linux (.so files) and Windows (.dll files).
Supported PHP versions: 7.4
, 8.0
, 8.1
and 8.2
.
You can also download the libraries from the website openai-php.com.
On Linux systems, the
libcurl
library must be installed.
Before installation, check the installed version of PHP (NTS - non-tread safe, ZTS - thread safe) on your system with the command:
php -v
Unzip the archive and enter the root directory, for example:
tar xf openai-php-binary-linux.tar.gz
cd openai-php-binary-linux
Now, you can use the PHP
, MODE
and ZTS
parameters in the make
command. PHP
means the PHP version (7.4, 8.0, 8.1, 8.2), MODE
means operating modes (cli, fpm or apache2) and ZTS
means thread safe (enable
) or non-thread safe (disable
) version.
The syntax for make
is:
make PHP=7.4,8.0,8.1,8.2 MODE=cli,fpm,apache2 ZTS=enable,disable
For example, to install the extension for PHP 8.2, execute the command:
- for PHP 8.2 ZTS (thread safe), CLI mode:
make PHP=8.2 MODE=cli ZTS=enable
- for PHP 8.2 NTS (non-thread safe), FPM mode:
make PHP=8.2 MODE=fpm ZTS=disable
By default, if MODE
parameter is not defined, the extension is installed for the PHP CLI
.
Done! You may need to reload the webserver.
To uninstall the extension, execute the command:
make PHP=7.4,8.0,8.1,8.2 MODE=cli,fpm,apache2 uninstall
Download the latest release of binaries for Windows. Find the directory where PHP is installed and then upload the extension to the ext
directory. For example, if you have installed the XAMPP on your C
drive, upload the extension to the C:\xampp\php\ext
directory.
Activate the extension by adding the following line to the php.ini
file:
extension=openai.dll
Use the phpinfo()
function to get detailed information about active extensions in PHP.
- Generate an
API key
on the website OpenAI. - Run the following code in a console or browser. Done!
<?php
namespace OpenAI;
use Exception;
$OpenAI = new OpenAI(api_key: "YOUR_API_KEY");
try
{
$response = $OpenAI->chat(request:
[
"model" => "gpt-3.5-turbo",
"messages" =>
[
"role" => "user",
"content" => "Do you know Elon Musk?"
]
], response: OpenAI::PHP_OPENAI_RESPONSE_ARRAY);
print_r($response);
}
catch (Exception $e)
{
echo("Hmm, something went wrong :(").PHP_EOL;
echo $e->getMessage();
}
unset($OpenAI);
The OpenAI PHP extension allows you to save all requests sent to the OpenAI system and all responses to log files. You can activate this function globally in the php.ini
file:
openai.log_enable = 1
openai.log_filepath = "./openai.log"
Enabling this feature may increase server load, so the optimal option is to activate logging by the user. An example of activating this function in PHP code by the user might be:
define("OPENAI_LOG_ENABLE", 1);
define("OPENAI_LOG_FILEPATH", "./openai.log");
The default response
parameter value is OpenAI::PHP_OPENAI_RESPONSE_ARRAY
. This means that all functions return data as arrays by default. To return data in JSON format, use the OpenAI::PHP_OPENAI_RESPONSE_JSON
constants.
$models = $OpenAI->models(
response: OpenAI::PHP_OPENAI_RESPONSE_JSON
);
In PHP 7 (and earlier versions), function parameters are declared with a dollar sign followed by the parameter name, like this:
<?php
namespace OpenAI;
$OpenAI = new OpenAI($api_key = "YOUR_API_KEY");
$models = $OpenAI->models(
$response = OpenAI::PHP_OPENAI_RESPONSE_ARRAY
);
Create image | Create image edit | Create image variation |
---|---|---|
![]() |
![]() |
![]() |
C++11
/C++14
/C++17
/C++20
compatible compiler- PHP-CPP library or PHP-CPP library for PHP 8
- OpenAI C++ library
- JSON for Modern C++ library
- libcurl
The following commands have been tested on Ubuntu 22.04.
If you are using Windows 11, you can use WSL to install Ubuntu.
- Log in as an administrator and update all libraries.
sudo -i
apt update
apt upgrade
cd /opt
- Install the development versions of
PHP
andlibcurl
.
apt install php8.1-dev
apt install libcurl4-gnutls-dev
- Download
OpenAI PHP
repo:
git clone https://github.com/mariuszduka/openai-php
cd openai-php
- Download and install
PHP-CPP
repo for PHP 8:
git clone https://github.com/fast-debug/PHP-CPP php-cpp
cd php-cpp
make
make install
make clean
cd ..
- Download the
OpenAI C++
andJSON for Modern C++
libraries:
wget -O libs/openai-cpp/openai.hpp https://raw.githubusercontent.com/olrea/openai-cpp/main/include/openai/openai.hpp
wget -O libs/openai-cpp/nlohmann/json.hpp https://raw.githubusercontent.com/nlohmann/json/develop/single_include/nlohmann/json.hpp
- Update
OpenAI C++
code forOpenAI PHP
support:
patch libs/openai-cpp/openai.hpp libs/openai.hpp.patch
- Build
OpenAI PHP
extension:
make
make MODE=cli install
The MODE
parameter can be cli
, fpm
or apache2
.
Done! You may need to reload the webserver.
- Check that the
OpenAI PHP
extension is active in PHP:
php -m | grep openai
- Run the openai-basic.php file with:
php openai-basic.php
Correct compilation of the PHP-CPP library causes the most problems, especially if we want to compile the library for Windows. Please check all pull requests as not all are accepted and you have to do it manually.
The optimal variant is to download the latest release of the library for Linux and Windows. All you have to do is download and activate the extension in your PHP configuration.
MIT License
Copyright (c) 2023 Mariusz Duka
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.