Introduction to ChatGPT-PHP
Overview
The ChatGPT-PHP project presents developers with a robust PHP SDK that seamlessly connects with both official and reverse-engineered ChatGPT APIs. This SDK is a result of the work done by the HaoZi Team, utilizing the foundational reconstructions made by @acheong08 in their ChatGPT API project.
Installation
To get started with ChatGPT-PHP, developers can easily include it in their projects using Composer with the following command:
composer require haozi-team/chatgpt-php
V1 Web ChatGPT
The V1 version is built to interact with the web-based ChatGPT, offering free access albeit with some limitations:
- Uses the endpoint from
chat.openai.com
. - Access may require bypassing Cloudflare.
- Free users face a limit of 50 requests per hour, while Plus accounts allow for up to 150 requests.
- Supports multi-account cycling to bypass access limits.
- An Arkose Token might be required, which the SDK fetches automatically using a special API.
Configuration and Authentication
- Create an OpenAI account and record your login credentials.
- Acquire an
access_token
by signing in and accessing your session details athttps://chat.openai.com/api/auth/session
. The token remains valid for 30 days.
Using the Developer API
Here’s a basic usage scenario with the V1 SDK:
<?php
use HaoZiTeam\ChatGPT\V1 as ChatGPTV1;
$chatGPT = new ChatGPTV1();
$chatGPT->addAccount('<your_access_token>');
$answers = $chatGPT->ask('Hello, how are you?');
foreach ($answers as $item) {
print_r($item);
}
For those needing a more tailored setup, the SDK supports setting a custom API endpoint:
<?php
use HaoZiTeam\ChatGPT\V1 as ChatGPTV1;
$chatGPT = new ChatGPTV1('https://chat.openai.com/backend-api/');
Further advanced configurations can be explored in the project's wiki.
V2 Official ChatGPT
The V2 version represents a more recent release by OpenAI, which offers more capabilities albeit at a cost. An API key is required, obtainable from the OpenAI platform.
Developer API in V2
A simple implementation with V2 may look like this:
<?php
use HaoZiTeam\ChatGPT\V2 as ChatGPTV2;
$chatGPT = new ChatGPTV2('sk-<your_api_key>');
$chatGPT->addMessage('You are ChatGPT, a large language model trained by OpenAI. Answer as concisely as possible.', 'system');
$answers = $chatGPT->ask('Hello, how are you?');
foreach ($answers as $item) {
print_r($item);
}
Developers can adjust the API endpoint by passing a custom URL and leverage the addMessage
functionality to maintain seamless conversational context.
More complex interactions, such as setting the stream
parameter for real-time response generation, illustrate the SDK’s versatility for dynamic applications.
Disclaimers
It’s crucial to note that ChatGPT-PHP is not an official OpenAI product. It is an independent project and any association with OpenAI should not be inferred.
Acknowledgments
The SDK owes its foundational structure to the original Python ChatGPT API developed by acheong08, whose contributions have been instrumental in making this PHP SDK a reality.