{"id":6036,"date":"2026-01-22T09:12:10","date_gmt":"2026-01-22T09:12:10","guid":{"rendered":"https:\/\/www.codexworld.com\/?p=6036"},"modified":"2026-01-22T09:20:11","modified_gmt":"2026-01-22T09:20:11","slug":"chatgpt-api-integration-in-php","status":"publish","type":"post","link":"https:\/\/www.codexworld.com\/chatgpt-api-integration-in-php\/","title":{"rendered":"ChatGPT API Integration in PHP"},"content":{"rendered":"<p>The <b>ChatGPT API<\/b>, provided by OpenAI, enables developers to access the powerful language model capabilities of ChatGPT. By integrating this API into your PHP applications, you can create interactive and intelligent conversational agents that can understand and respond to user inputs in a human-like manner.<\/p>\n<p>Integrating the ChatGPT API into a PHP application allows you to build AI-powered features such as chatbots, virtual assistants, content generators, and customer support tools. In this tutorial, we will learn how to <b>integrate the ChatGPT API in PHP<\/b> using OpenAI\u2019s Chat API to build AI-powered applications.<\/p>\n<h2>\ud83d\udd25 What You Will Learn<\/h2>\n<ul>\n<li>How the ChatGPT API works<\/li>\n<li>How to set up a PHP environment for API integration<\/li>\n<li>How to obtain and configure the OpenAI API key<\/li>\n<li>How to configure OpenAI API in PHP<\/li>\n<li>Building a ChatGPT client in PHP<\/li>\n<li>How to send requests and handle responses from the ChatGPT API<\/li>\n<li>Handling conversations and system prompts<\/li>\n<li>Creating an AJAX-powered chat interface for user interaction<\/li>\n<li>Error handling and best practices<\/li>\n<\/ul>\n<h2>\ud83d\udccb Prerequisites<\/h2>\n<p>Before you begin, ensure you have the following:<\/p>\n<ul>\n<li>PHP 7.4 or later<\/li>\n<li>PHP cURL extension enabled<\/li>\n<li>An OpenAI API Key<\/li>\n<li>A local or live web server (XAMPP, WAMP, LAMP, etc.)<\/li>\n<\/ul>\n<h2>\ud83d\udd11 Get Your OpenAI API Key<\/h2>\n<p>To interact with the ChatGPT API, you need an API key from OpenAI. Follow these steps to obtain your API key:<\/p>\n<ol>\n<li>Sign up or log in to your OpenAI account at <a href=\"https:\/\/platform.openai.com\/\">OpenAI Platform<\/a>.<\/li>\n<li>Navigate to the <b>API Keys<\/b> section.<\/li>\n<li>Create a new API key and copy it.<\/li>\n<\/ol>\n<h2>\ud83d\udcc1 Project Structure Overview<\/h2>\n<p>Before diving into the code, let&#8217;s take a look at the project structure:<\/p>\n<pre>\r\nchatgpt_api_integration_in_php\/\r\n\u251c\u2500\u2500 config.php\r\n\u251c\u2500\u2500 ChatGPTClient.php\r\n\u251c\u2500\u2500 api.php\r\n\u2514\u2500\u2500 index.html\r\n<\/pre>\n<p>Each file has a specific responsibility, making the integration easy to maintain and extend.<\/p>\n<ul>\n<li><b>config.php<\/b>: Contains configuration settings, including the OpenAI API key and model selection.<\/li>\n<li><b>ChatGPTClient.php<\/b>: A PHP class that handles communication with the ChatGPT API.<\/li>\n<li><b>api.php<\/b>: The backend script that processes AJAX requests from the frontend and interacts with the ChatGPTClient.<\/li>\n<li><b>index.html<\/b>: The frontend interface (chat UI) where users can interact with the ChatGPT-powered chatbot.<\/li>\n<\/ul>\n<h4>\ud83d\ude80 Let&#8217;s get started with ChatGPT API Integration in PHP step by step, using a ready-made and production-ready script.<\/h4>\n<h2>API Configuration (config.php)<\/h2>\n<p>The <code>config.php<\/code> file contains essential configuration settings for the ChatGPT API integration. Here, you will define your OpenAI API key, select the model to use, and set other parameters like temperature and maximum tokens.<\/p>\n<pre><span style=\"color: #0000BB\">&lt;?php <br \/><\/span><span style=\"color: #FF8000\">\/\/&nbsp;Your&nbsp;OpenAI&nbsp;API&nbsp;Key&nbsp;(get&nbsp;it&nbsp;from&nbsp;https:\/\/platform.openai.com\/api-keys) <br \/><\/span><span style=\"color: #0000BB\">define<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #DD0000\">'OPENAI_API_KEY'<\/span><span style=\"color: #007700\">,&nbsp;<\/span><span style=\"color: #DD0000\">'your-api-key-here'<\/span><span style=\"color: #007700\">); <br \/> <br \/><\/span><span style=\"color: #FF8000\">\/\/&nbsp;API&nbsp;Endpoint <br \/><\/span><span style=\"color: #0000BB\">define<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #DD0000\">'OPENAI_API_ENDPOINT'<\/span><span style=\"color: #007700\">,&nbsp;<\/span><span style=\"color: #DD0000\">'https:\/\/api.openai.com\/v1\/chat\/completions'<\/span><span style=\"color: #007700\">); <br \/> <br \/><\/span><span style=\"color: #FF8000\">\/\/&nbsp;Model&nbsp;to&nbsp;use <br \/><\/span><span style=\"color: #0000BB\">define<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #DD0000\">'OPENAI_MODEL'<\/span><span style=\"color: #007700\">,&nbsp;<\/span><span style=\"color: #DD0000\">'gpt-3.5-turbo'<\/span><span style=\"color: #007700\">);&nbsp;<\/span><span style=\"color: #FF8000\">\/\/gpt-5.2,&nbsp;gpt-5-nano <br \/> <br \/>\/\/&nbsp;Request&nbsp;timeout&nbsp;(in&nbsp;seconds) <br \/><\/span><span style=\"color: #0000BB\">define<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #DD0000\">'REQUEST_TIMEOUT'<\/span><span style=\"color: #007700\">,&nbsp;<\/span><span style=\"color: #0000BB\">30<\/span><span style=\"color: #007700\">); <br \/> <br \/><\/span><span style=\"color: #FF8000\">\/\/&nbsp;Temperature&nbsp;(controls&nbsp;randomness:&nbsp;0&nbsp;=&nbsp;deterministic,&nbsp;1&nbsp;=&nbsp;random) <br \/><\/span><span style=\"color: #0000BB\">define<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #DD0000\">'TEMPERATURE'<\/span><span style=\"color: #007700\">,&nbsp;<\/span><span style=\"color: #0000BB\">0.7<\/span><span style=\"color: #007700\">); <br \/> <br \/><\/span><span style=\"color: #FF8000\">\/\/&nbsp;Maximum&nbsp;tokens&nbsp;in&nbsp;response <br \/><\/span><span style=\"color: #0000BB\">define<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #DD0000\">'MAX_TOKENS'<\/span><span style=\"color: #007700\">,&nbsp;<\/span><span style=\"color: #0000BB\">2000<\/span><span style=\"color: #007700\">); <br \/><\/span><span style=\"color: #0000BB\">?&gt;<\/span><\/pre>\n<p><i>Make sure to replace <code>'your-api-key-here'<\/code> with your actual OpenAI API key obtained from the OpenAI platform.<\/i><\/p>\n<h2>ChatGPT Client Class (ChatGPTClient.php)<\/h2>\n<p>The <code>ChatGPTClient.php<\/code> file contains a PHP class that encapsulates the logic for communicating with the ChatGPT API. This class handles sending requests and processing responses.<\/p>\n<p>Some of the key features of this class include:<\/p>\n<ul>\n<li>Initialization with API key, model, timeout, temperature, and max tokens.<\/li>\n<li>Building request payloads for the ChatGPT API.<\/li>\n<li>Sending HTTP requests to the ChatGPT API via cURL.<\/li>\n<li>Error handling and response processing.<\/li>\n<li>Support for conversation history and system roles.<\/li>\n<\/ul>\n<p><b>Core Method: sendMessage()<\/b><br \/>\nThis method sends a user prompt along with optional conversation history and returns the AI response.<br \/>\nIt automatically:<\/p>\n<ul>\n<li>Prepends system prompts<\/li>\n<li>Appends user messages<\/li>\n<li>Parses the API response<\/li>\n<\/ul>\n<pre><span style=\"color: #0000BB\">&lt;?php <br \/><\/span><span style=\"color: #007700\">class&nbsp;<\/span><span style=\"color: #0000BB\">ChatGPTClient <br \/><\/span><span style=\"color: #007700\">{ <br \/>&nbsp;&nbsp;&nbsp;&nbsp;private&nbsp;<\/span><span style=\"color: #0000BB\">$apiKey<\/span><span style=\"color: #007700\">; <br \/>&nbsp;&nbsp;&nbsp;&nbsp;private&nbsp;<\/span><span style=\"color: #0000BB\">$endpoint<\/span><span style=\"color: #007700\">; <br \/>&nbsp;&nbsp;&nbsp;&nbsp;private&nbsp;<\/span><span style=\"color: #0000BB\">$model<\/span><span style=\"color: #007700\">; <br \/>&nbsp;&nbsp;&nbsp;&nbsp;private&nbsp;<\/span><span style=\"color: #0000BB\">$timeout<\/span><span style=\"color: #007700\">; <br \/>&nbsp;&nbsp;&nbsp;&nbsp;private&nbsp;<\/span><span style=\"color: #0000BB\">$temperature<\/span><span style=\"color: #007700\">; <br \/>&nbsp;&nbsp;&nbsp;&nbsp;private&nbsp;<\/span><span style=\"color: #0000BB\">$maxTokens<\/span><span style=\"color: #007700\">; <br \/>&nbsp;&nbsp;&nbsp;&nbsp;private&nbsp;<\/span><span style=\"color: #0000BB\">$lastError<\/span><span style=\"color: #007700\">; <br \/> <br \/>&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #FF8000\">\/** <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;Constructor <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp; <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;@param&nbsp;string&nbsp;$apiKey&nbsp;OpenAI&nbsp;API&nbsp;Key <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;@param&nbsp;string&nbsp;$model&nbsp;Model&nbsp;to&nbsp;use&nbsp;(default:&nbsp;gpt-3.5-turbo) <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;@param&nbsp;int&nbsp;$timeout&nbsp;Request&nbsp;timeout&nbsp;in&nbsp;seconds <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;@param&nbsp;float&nbsp;$temperature&nbsp;Temperature&nbsp;for&nbsp;response&nbsp;(0-2) <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;@param&nbsp;int&nbsp;$maxTokens&nbsp;Maximum&nbsp;tokens&nbsp;in&nbsp;response <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*\/ <br \/>&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #007700\">public&nbsp;function&nbsp;<\/span><span style=\"color: #0000BB\">__construct<\/span><span style=\"color: #007700\">( <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #0000BB\">$apiKey<\/span><span style=\"color: #007700\">, <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #0000BB\">$model&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;<\/span><span style=\"color: #DD0000\">'gpt-3.5-turbo'<\/span><span style=\"color: #007700\">, <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #0000BB\">$timeout&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;<\/span><span style=\"color: #0000BB\">30<\/span><span style=\"color: #007700\">, <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #0000BB\">$temperature&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;<\/span><span style=\"color: #0000BB\">0.7<\/span><span style=\"color: #007700\">, <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #0000BB\">$maxTokens&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;<\/span><span style=\"color: #0000BB\">2000 <br \/>&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #007700\">)&nbsp;{ <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #0000BB\">$this<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">apiKey&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;<\/span><span style=\"color: #0000BB\">$apiKey<\/span><span style=\"color: #007700\">; <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #0000BB\">$this<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">model&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;<\/span><span style=\"color: #0000BB\">$model<\/span><span style=\"color: #007700\">; <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #0000BB\">$this<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">endpoint&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;<\/span><span style=\"color: #DD0000\">'https:\/\/api.openai.com\/v1\/chat\/completions'<\/span><span style=\"color: #007700\">; <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #0000BB\">$this<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">timeout&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;<\/span><span style=\"color: #0000BB\">$timeout<\/span><span style=\"color: #007700\">; <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #0000BB\">$this<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">temperature&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;<\/span><span style=\"color: #0000BB\">$temperature<\/span><span style=\"color: #007700\">; <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #0000BB\">$this<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">maxTokens&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;<\/span><span style=\"color: #0000BB\">$maxTokens<\/span><span style=\"color: #007700\">; <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #0000BB\">$this<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">lastError&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;<\/span><span style=\"color: #0000BB\">null<\/span><span style=\"color: #007700\">; <br \/>&nbsp;&nbsp;&nbsp;&nbsp;} <br \/> <br \/>&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #FF8000\">\/** <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;Send&nbsp;a&nbsp;message&nbsp;to&nbsp;ChatGPT&nbsp;and&nbsp;get&nbsp;a&nbsp;response <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp; <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;@param&nbsp;string&nbsp;$message&nbsp;User&nbsp;message <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;@param&nbsp;array&nbsp;$conversationHistory&nbsp;Previous&nbsp;messages&nbsp;for&nbsp;context&nbsp;(optional) <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;@return&nbsp;string|false&nbsp;Response&nbsp;from&nbsp;ChatGPT&nbsp;or&nbsp;false&nbsp;on&nbsp;failure <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*\/ <br \/>&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #007700\">public&nbsp;function&nbsp;<\/span><span style=\"color: #0000BB\">sendMessage<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">$message<\/span><span style=\"color: #007700\">,&nbsp;<\/span><span style=\"color: #0000BB\">$conversationHistory&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;[]) <br \/>&nbsp;&nbsp;&nbsp;&nbsp;{ <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;try&nbsp;{ <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #FF8000\">\/\/&nbsp;Prepare&nbsp;messages&nbsp;array <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #0000BB\">$messages&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;<\/span><span style=\"color: #0000BB\">$conversationHistory<\/span><span style=\"color: #007700\">; <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #0000BB\">$messages<\/span><span style=\"color: #007700\">[]&nbsp;=&nbsp;[ <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #DD0000\">'role'&nbsp;<\/span><span style=\"color: #007700\">=&gt;&nbsp;<\/span><span style=\"color: #DD0000\">'user'<\/span><span style=\"color: #007700\">, <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #DD0000\">'content'&nbsp;<\/span><span style=\"color: #007700\">=&gt;&nbsp;<\/span><span style=\"color: #0000BB\">$message <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #007700\">]; <br \/> <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #FF8000\">\/\/&nbsp;Prepare&nbsp;request&nbsp;payload <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #0000BB\">$payload&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;[ <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #DD0000\">'model'&nbsp;<\/span><span style=\"color: #007700\">=&gt;&nbsp;<\/span><span style=\"color: #0000BB\">$this<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">model<\/span><span style=\"color: #007700\">, <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #DD0000\">'messages'&nbsp;<\/span><span style=\"color: #007700\">=&gt;&nbsp;<\/span><span style=\"color: #0000BB\">$messages<\/span><span style=\"color: #007700\">, <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #DD0000\">'temperature'&nbsp;<\/span><span style=\"color: #007700\">=&gt;&nbsp;<\/span><span style=\"color: #0000BB\">$this<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">temperature<\/span><span style=\"color: #007700\">, <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #DD0000\">'max_tokens'&nbsp;<\/span><span style=\"color: #007700\">=&gt;&nbsp;<\/span><span style=\"color: #0000BB\">$this<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">maxTokens <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #007700\">]; <br \/> <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #FF8000\">\/\/&nbsp;Send&nbsp;request <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #0000BB\">$response&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;<\/span><span style=\"color: #0000BB\">$this<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">makeRequest<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">$payload<\/span><span style=\"color: #007700\">); <br \/> <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(<\/span><span style=\"color: #0000BB\">$response&nbsp;<\/span><span style=\"color: #007700\">===&nbsp;<\/span><span style=\"color: #0000BB\">false<\/span><span style=\"color: #007700\">)&nbsp;{ <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;<\/span><span style=\"color: #0000BB\">false<\/span><span style=\"color: #007700\">; <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} <br \/> <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #FF8000\">\/\/&nbsp;Extract&nbsp;response&nbsp;content <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #007700\">if&nbsp;(isset(<\/span><span style=\"color: #0000BB\">$response<\/span><span style=\"color: #007700\">[<\/span><span style=\"color: #DD0000\">'choices'<\/span><span style=\"color: #007700\">][<\/span><span style=\"color: #0000BB\">0<\/span><span style=\"color: #007700\">][<\/span><span style=\"color: #DD0000\">'message'<\/span><span style=\"color: #007700\">][<\/span><span style=\"color: #DD0000\">'content'<\/span><span style=\"color: #007700\">]))&nbsp;{ <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;<\/span><span style=\"color: #0000BB\">trim<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">$response<\/span><span style=\"color: #007700\">[<\/span><span style=\"color: #DD0000\">'choices'<\/span><span style=\"color: #007700\">][<\/span><span style=\"color: #0000BB\">0<\/span><span style=\"color: #007700\">][<\/span><span style=\"color: #DD0000\">'message'<\/span><span style=\"color: #007700\">][<\/span><span style=\"color: #DD0000\">'content'<\/span><span style=\"color: #007700\">]); <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} <br \/> <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #0000BB\">$this<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">lastError&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;<\/span><span style=\"color: #DD0000\">'Invalid&nbsp;response&nbsp;format&nbsp;from&nbsp;API'<\/span><span style=\"color: #007700\">; <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;<\/span><span style=\"color: #0000BB\">false<\/span><span style=\"color: #007700\">; <br \/> <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;catch&nbsp;(<\/span><span style=\"color: #0000BB\">Exception&nbsp;$e<\/span><span style=\"color: #007700\">)&nbsp;{ <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #0000BB\">$this<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">lastError&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;<\/span><span style=\"color: #DD0000\">'Exception:&nbsp;'&nbsp;<\/span><span style=\"color: #007700\">.&nbsp;<\/span><span style=\"color: #0000BB\">$e<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">getMessage<\/span><span style=\"color: #007700\">(); <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;<\/span><span style=\"color: #0000BB\">false<\/span><span style=\"color: #007700\">; <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} <br \/>&nbsp;&nbsp;&nbsp;&nbsp;} <br \/> <br \/>&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #FF8000\">\/** <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;Make&nbsp;HTTP&nbsp;request&nbsp;to&nbsp;OpenAI&nbsp;API <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp; <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;@param&nbsp;array&nbsp;$payload&nbsp;Request&nbsp;payload <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;@return&nbsp;array|false&nbsp;Response&nbsp;data&nbsp;or&nbsp;false&nbsp;on&nbsp;failure <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*\/ <br \/>&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #007700\">private&nbsp;function&nbsp;<\/span><span style=\"color: #0000BB\">makeRequest<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">$payload<\/span><span style=\"color: #007700\">) <br \/>&nbsp;&nbsp;&nbsp;&nbsp;{ <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #0000BB\">$ch&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;<\/span><span style=\"color: #0000BB\">curl_init<\/span><span style=\"color: #007700\">(); <br \/> <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #0000BB\">$options&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;[ <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #0000BB\">CURLOPT_URL&nbsp;<\/span><span style=\"color: #007700\">=&gt;&nbsp;<\/span><span style=\"color: #0000BB\">$this<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">endpoint<\/span><span style=\"color: #007700\">, <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #0000BB\">CURLOPT_RETURNTRANSFER&nbsp;<\/span><span style=\"color: #007700\">=&gt;&nbsp;<\/span><span style=\"color: #0000BB\">true<\/span><span style=\"color: #007700\">, <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #0000BB\">CURLOPT_TIMEOUT&nbsp;<\/span><span style=\"color: #007700\">=&gt;&nbsp;<\/span><span style=\"color: #0000BB\">$this<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">timeout<\/span><span style=\"color: #007700\">, <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #0000BB\">CURLOPT_HTTPHEADER&nbsp;<\/span><span style=\"color: #007700\">=&gt;&nbsp;[ <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #DD0000\">'Content-Type:&nbsp;application\/json'<\/span><span style=\"color: #007700\">, <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #DD0000\">'Authorization:&nbsp;Bearer&nbsp;'&nbsp;<\/span><span style=\"color: #007700\">.&nbsp;<\/span><span style=\"color: #0000BB\">$this<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">apiKey <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #007700\">], <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #0000BB\">CURLOPT_POST&nbsp;<\/span><span style=\"color: #007700\">=&gt;&nbsp;<\/span><span style=\"color: #0000BB\">true<\/span><span style=\"color: #007700\">, <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #0000BB\">CURLOPT_POSTFIELDS&nbsp;<\/span><span style=\"color: #007700\">=&gt;&nbsp;<\/span><span style=\"color: #0000BB\">json_encode<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">$payload<\/span><span style=\"color: #007700\">) <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;]; <br \/> <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #0000BB\">curl_setopt_array<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">$ch<\/span><span style=\"color: #007700\">,&nbsp;<\/span><span style=\"color: #0000BB\">$options<\/span><span style=\"color: #007700\">); <br \/> <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #0000BB\">$response&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;<\/span><span style=\"color: #0000BB\">curl_exec<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">$ch<\/span><span style=\"color: #007700\">); <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #0000BB\">$httpCode&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;<\/span><span style=\"color: #0000BB\">curl_getinfo<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">$ch<\/span><span style=\"color: #007700\">,&nbsp;<\/span><span style=\"color: #0000BB\">CURLINFO_HTTP_CODE<\/span><span style=\"color: #007700\">); <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #0000BB\">$curlError&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;<\/span><span style=\"color: #0000BB\">curl_error<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">$ch<\/span><span style=\"color: #007700\">); <br \/> <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #0000BB\">curl_close<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">$ch<\/span><span style=\"color: #007700\">); <br \/> <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #FF8000\">\/\/&nbsp;Check&nbsp;for&nbsp;cURL&nbsp;errors <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #007700\">if&nbsp;(<\/span><span style=\"color: #0000BB\">$curlError<\/span><span style=\"color: #007700\">)&nbsp;{ <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #0000BB\">$this<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">lastError&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;<\/span><span style=\"color: #DD0000\">'cURL&nbsp;error:&nbsp;'&nbsp;<\/span><span style=\"color: #007700\">.&nbsp;<\/span><span style=\"color: #0000BB\">$curlError<\/span><span style=\"color: #007700\">; <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;<\/span><span style=\"color: #0000BB\">false<\/span><span style=\"color: #007700\">; <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} <br \/> <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #FF8000\">\/\/&nbsp;Check&nbsp;HTTP&nbsp;status&nbsp;code <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #007700\">if&nbsp;(<\/span><span style=\"color: #0000BB\">$httpCode&nbsp;<\/span><span style=\"color: #007700\">!==&nbsp;<\/span><span style=\"color: #0000BB\">200<\/span><span style=\"color: #007700\">)&nbsp;{ <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #0000BB\">$this<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">handleHttpError<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">$httpCode<\/span><span style=\"color: #007700\">,&nbsp;<\/span><span style=\"color: #0000BB\">$response<\/span><span style=\"color: #007700\">); <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;<\/span><span style=\"color: #0000BB\">false<\/span><span style=\"color: #007700\">; <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} <br \/> <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #FF8000\">\/\/&nbsp;Decode&nbsp;response <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #0000BB\">$decodedResponse&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;<\/span><span style=\"color: #0000BB\">json_decode<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">$response<\/span><span style=\"color: #007700\">,&nbsp;<\/span><span style=\"color: #0000BB\">true<\/span><span style=\"color: #007700\">); <br \/> <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(<\/span><span style=\"color: #0000BB\">json_last_error<\/span><span style=\"color: #007700\">()&nbsp;!==&nbsp;<\/span><span style=\"color: #0000BB\">JSON_ERROR_NONE<\/span><span style=\"color: #007700\">)&nbsp;{ <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #0000BB\">$this<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">lastError&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;<\/span><span style=\"color: #DD0000\">'JSON&nbsp;decode&nbsp;error:&nbsp;'&nbsp;<\/span><span style=\"color: #007700\">.&nbsp;<\/span><span style=\"color: #0000BB\">json_last_error_msg<\/span><span style=\"color: #007700\">(); <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;<\/span><span style=\"color: #0000BB\">false<\/span><span style=\"color: #007700\">; <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} <br \/> <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #FF8000\">\/\/&nbsp;Check&nbsp;for&nbsp;API&nbsp;errors <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #007700\">if&nbsp;(isset(<\/span><span style=\"color: #0000BB\">$decodedResponse<\/span><span style=\"color: #007700\">[<\/span><span style=\"color: #DD0000\">'error'<\/span><span style=\"color: #007700\">]))&nbsp;{ <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #0000BB\">$this<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">lastError&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;<\/span><span style=\"color: #DD0000\">'API&nbsp;Error:&nbsp;'&nbsp;<\/span><span style=\"color: #007700\">.&nbsp;<\/span><span style=\"color: #0000BB\">$decodedResponse<\/span><span style=\"color: #007700\">[<\/span><span style=\"color: #DD0000\">'error'<\/span><span style=\"color: #007700\">][<\/span><span style=\"color: #DD0000\">'message'<\/span><span style=\"color: #007700\">]; <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;<\/span><span style=\"color: #0000BB\">false<\/span><span style=\"color: #007700\">; <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} <br \/> <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;<\/span><span style=\"color: #0000BB\">$decodedResponse<\/span><span style=\"color: #007700\">; <br \/>&nbsp;&nbsp;&nbsp;&nbsp;} <br \/> <br \/>&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #FF8000\">\/** <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;Handle&nbsp;HTTP&nbsp;errors&nbsp;from&nbsp;API <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp; <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;@param&nbsp;int&nbsp;$httpCode&nbsp;HTTP&nbsp;status&nbsp;code <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;@param&nbsp;string&nbsp;$response&nbsp;Response&nbsp;body <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*\/ <br \/>&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #007700\">private&nbsp;function&nbsp;<\/span><span style=\"color: #0000BB\">handleHttpError<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">$httpCode<\/span><span style=\"color: #007700\">,&nbsp;<\/span><span style=\"color: #0000BB\">$response<\/span><span style=\"color: #007700\">) <br \/>&nbsp;&nbsp;&nbsp;&nbsp;{ <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #0000BB\">$errorMessages&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;[ <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #0000BB\">400&nbsp;<\/span><span style=\"color: #007700\">=&gt;&nbsp;<\/span><span style=\"color: #DD0000\">'Bad&nbsp;Request&nbsp;-&nbsp;Invalid&nbsp;parameters'<\/span><span style=\"color: #007700\">, <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #0000BB\">401&nbsp;<\/span><span style=\"color: #007700\">=&gt;&nbsp;<\/span><span style=\"color: #DD0000\">'Unauthorized&nbsp;-&nbsp;Invalid&nbsp;API&nbsp;key'<\/span><span style=\"color: #007700\">, <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #0000BB\">403&nbsp;<\/span><span style=\"color: #007700\">=&gt;&nbsp;<\/span><span style=\"color: #DD0000\">'Forbidden&nbsp;-&nbsp;Access&nbsp;denied'<\/span><span style=\"color: #007700\">, <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #0000BB\">429&nbsp;<\/span><span style=\"color: #007700\">=&gt;&nbsp;<\/span><span style=\"color: #DD0000\">'Too&nbsp;Many&nbsp;Requests&nbsp;-&nbsp;Rate&nbsp;limit&nbsp;exceeded'<\/span><span style=\"color: #007700\">, <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #0000BB\">500&nbsp;<\/span><span style=\"color: #007700\">=&gt;&nbsp;<\/span><span style=\"color: #DD0000\">'Internal&nbsp;Server&nbsp;Error'<\/span><span style=\"color: #007700\">, <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #0000BB\">503&nbsp;<\/span><span style=\"color: #007700\">=&gt;&nbsp;<\/span><span style=\"color: #DD0000\">'Service&nbsp;Unavailable' <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #007700\">]; <br \/> <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #0000BB\">$message&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;<\/span><span style=\"color: #0000BB\">$errorMessages<\/span><span style=\"color: #007700\">[<\/span><span style=\"color: #0000BB\">$httpCode<\/span><span style=\"color: #007700\">]&nbsp;??&nbsp;<\/span><span style=\"color: #DD0000\">\"HTTP&nbsp;Error&nbsp;<\/span><span style=\"color: #0000BB\">$httpCode<\/span><span style=\"color: #DD0000\">\"<\/span><span style=\"color: #007700\">; <br \/> <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #FF8000\">\/\/&nbsp;Try&nbsp;to&nbsp;get&nbsp;more&nbsp;details&nbsp;from&nbsp;response <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #0000BB\">$decoded&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;<\/span><span style=\"color: #0000BB\">json_decode<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">$response<\/span><span style=\"color: #007700\">,&nbsp;<\/span><span style=\"color: #0000BB\">true<\/span><span style=\"color: #007700\">); <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(isset(<\/span><span style=\"color: #0000BB\">$decoded<\/span><span style=\"color: #007700\">[<\/span><span style=\"color: #DD0000\">'error'<\/span><span style=\"color: #007700\">][<\/span><span style=\"color: #DD0000\">'message'<\/span><span style=\"color: #007700\">]))&nbsp;{ <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #0000BB\">$message&nbsp;<\/span><span style=\"color: #007700\">.=&nbsp;<\/span><span style=\"color: #DD0000\">':&nbsp;'&nbsp;<\/span><span style=\"color: #007700\">.&nbsp;<\/span><span style=\"color: #0000BB\">$decoded<\/span><span style=\"color: #007700\">[<\/span><span style=\"color: #DD0000\">'error'<\/span><span style=\"color: #007700\">][<\/span><span style=\"color: #DD0000\">'message'<\/span><span style=\"color: #007700\">]; <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} <br \/> <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #0000BB\">$this<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">lastError&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;<\/span><span style=\"color: #0000BB\">$message<\/span><span style=\"color: #007700\">; <br \/>&nbsp;&nbsp;&nbsp;&nbsp;} <br \/> <br \/>&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #FF8000\">\/** <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;Get&nbsp;the&nbsp;last&nbsp;error <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp; <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;@return&nbsp;string|null&nbsp;Last&nbsp;error&nbsp;message&nbsp;or&nbsp;null&nbsp;if&nbsp;no&nbsp;error <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*\/ <br \/>&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #007700\">public&nbsp;function&nbsp;<\/span><span style=\"color: #0000BB\">getLastError<\/span><span style=\"color: #007700\">() <br \/>&nbsp;&nbsp;&nbsp;&nbsp;{ <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;<\/span><span style=\"color: #0000BB\">$this<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">lastError<\/span><span style=\"color: #007700\">; <br \/>&nbsp;&nbsp;&nbsp;&nbsp;} <br \/> <br \/>&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #FF8000\">\/** <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;Test&nbsp;the&nbsp;API&nbsp;connection <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp; <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;@return&nbsp;bool&nbsp;True&nbsp;if&nbsp;connection&nbsp;successful,&nbsp;false&nbsp;otherwise <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*\/ <br \/>&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #007700\">public&nbsp;function&nbsp;<\/span><span style=\"color: #0000BB\">testConnection<\/span><span style=\"color: #007700\">() <br \/>&nbsp;&nbsp;&nbsp;&nbsp;{ <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #0000BB\">$response&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;<\/span><span style=\"color: #0000BB\">$this<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">sendMessage<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #DD0000\">'Hello'<\/span><span style=\"color: #007700\">); <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;<\/span><span style=\"color: #0000BB\">$response&nbsp;<\/span><span style=\"color: #007700\">!==&nbsp;<\/span><span style=\"color: #0000BB\">false<\/span><span style=\"color: #007700\">; <br \/>&nbsp;&nbsp;&nbsp;&nbsp;} <br \/> <br \/>&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #FF8000\">\/** <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;Clear&nbsp;conversation&nbsp;history <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp; <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;@return&nbsp;void <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*\/ <br \/>&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #007700\">public&nbsp;function&nbsp;<\/span><span style=\"color: #0000BB\">clearHistory<\/span><span style=\"color: #007700\">() <br \/>&nbsp;&nbsp;&nbsp;&nbsp;{ <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #FF8000\">\/\/&nbsp;This&nbsp;is&nbsp;handled&nbsp;in&nbsp;the&nbsp;application&nbsp;logic <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/&nbsp;This&nbsp;method&nbsp;is&nbsp;here&nbsp;for&nbsp;API&nbsp;completeness <br \/>&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #007700\">} <br \/>} <br \/><\/span><span style=\"color: #0000BB\">?&gt;<\/span><\/pre>\n<h4>ChatGPTClient Class Usage Examples<\/h4>\n<p>The following examples demonstrate how to use the <code>ChatGPTClient<\/code> class to interact with the OpenAI ChatGPT API. Each example includes code snippets and explanations to help you understand how to implement various functionalities.<\/p>\n<p>First, ensure you include the necessary files and initialize the <code>ChatGPTClient<\/code> with your OpenAI API key:<\/p>\n<pre><span style=\"color: #FF8000\">\/\/&nbsp;Include&nbsp;configuration&nbsp;and&nbsp;client&nbsp;class <br \/><\/span><span style=\"color: #007700\">require_once&nbsp;<\/span><span style=\"color: #DD0000\">'config.php'<\/span><span style=\"color: #007700\">; <br \/>require_once&nbsp;<\/span><span style=\"color: #DD0000\">'ChatGPTClient.php'<\/span><span style=\"color: #007700\">; <br \/> <br \/><\/span><span style=\"color: #FF8000\">\/\/&nbsp;Initialize&nbsp;the&nbsp;ChatGPT&nbsp;client <br \/><\/span><span style=\"color: #0000BB\">$client&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;new&nbsp;<\/span><span style=\"color: #0000BB\">ChatGPTClient<\/span><span style=\"color: #007700\">( <br \/>&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #0000BB\">OPENAI_API_KEY<\/span><span style=\"color: #007700\">, <br \/>&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #0000BB\">OPENAI_MODEL<\/span><span style=\"color: #007700\">, <br \/>&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #0000BB\">REQUEST_TIMEOUT<\/span><span style=\"color: #007700\">, <br \/>&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #0000BB\">TEMPERATURE<\/span><span style=\"color: #007700\">, <br \/>&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #0000BB\">MAX_TOKENS <br \/><\/span><span style=\"color: #007700\">);<\/span><\/pre>\n<p><b>Example 1: Simple message<\/b><\/p>\n<pre><span style=\"color: #0000BB\">$response&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;<\/span><span style=\"color: #0000BB\">$client<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">sendMessage<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #DD0000\">'What&nbsp;is&nbsp;the&nbsp;capital&nbsp;of&nbsp;France?'<\/span><span style=\"color: #007700\">); <br \/>if&nbsp;(<\/span><span style=\"color: #0000BB\">$response<\/span><span style=\"color: #007700\">)&nbsp;{ <br \/>&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;<\/span><span style=\"color: #DD0000\">\"Q:&nbsp;What&nbsp;is&nbsp;the&nbsp;capital&nbsp;of&nbsp;France?\\n\"<\/span><span style=\"color: #007700\">; <br \/>&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;<\/span><span style=\"color: #DD0000\">\"A:&nbsp;\"&nbsp;<\/span><span style=\"color: #007700\">.&nbsp;<\/span><span style=\"color: #0000BB\">$response&nbsp;<\/span><span style=\"color: #007700\">.&nbsp;<\/span><span style=\"color: #DD0000\">\"\\n\\n\"<\/span><span style=\"color: #007700\">; <br \/>}&nbsp;else&nbsp;{ <br \/>&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;<\/span><span style=\"color: #DD0000\">\"Error:&nbsp;\"&nbsp;<\/span><span style=\"color: #007700\">.&nbsp;<\/span><span style=\"color: #0000BB\">$client<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">getLastError<\/span><span style=\"color: #007700\">()&nbsp;.&nbsp;<\/span><span style=\"color: #DD0000\">\"\\n\\n\"<\/span><span style=\"color: #007700\">; <br \/>}<\/span><\/pre>\n<p><b>Example 2: Conversation with history<\/b><\/p>\n<pre><span style=\"color: #0000BB\">$conversationHistory&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;[]; <br \/> <br \/><\/span><span style=\"color: #FF8000\">\/\/&nbsp;First&nbsp;message <br \/><\/span><span style=\"color: #0000BB\">$userMessage1&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;<\/span><span style=\"color: #DD0000\">\"Tell&nbsp;me&nbsp;about&nbsp;machine&nbsp;learning\"<\/span><span style=\"color: #007700\">; <br \/><\/span><span style=\"color: #0000BB\">$response1&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;<\/span><span style=\"color: #0000BB\">$client<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">sendMessage<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">$userMessage1<\/span><span style=\"color: #007700\">,&nbsp;<\/span><span style=\"color: #0000BB\">$conversationHistory<\/span><span style=\"color: #007700\">); <br \/>if&nbsp;(<\/span><span style=\"color: #0000BB\">$response1<\/span><span style=\"color: #007700\">)&nbsp;{ <br \/>&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;<\/span><span style=\"color: #DD0000\">\"You:&nbsp;\"&nbsp;<\/span><span style=\"color: #007700\">.&nbsp;<\/span><span style=\"color: #0000BB\">$userMessage1&nbsp;<\/span><span style=\"color: #007700\">.&nbsp;<\/span><span style=\"color: #DD0000\">\"\\n\"<\/span><span style=\"color: #007700\">; <br \/>&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;<\/span><span style=\"color: #DD0000\">\"Bot:&nbsp;\"&nbsp;<\/span><span style=\"color: #007700\">.&nbsp;<\/span><span style=\"color: #0000BB\">$response1&nbsp;<\/span><span style=\"color: #007700\">.&nbsp;<\/span><span style=\"color: #DD0000\">\"\\n\\n\"<\/span><span style=\"color: #007700\">; <br \/>&nbsp;&nbsp;&nbsp;&nbsp; <br \/>&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #FF8000\">\/\/&nbsp;Add&nbsp;to&nbsp;history <br \/>&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #0000BB\">$conversationHistory<\/span><span style=\"color: #007700\">[]&nbsp;=&nbsp;[<\/span><span style=\"color: #DD0000\">'role'&nbsp;<\/span><span style=\"color: #007700\">=&gt;&nbsp;<\/span><span style=\"color: #DD0000\">'user'<\/span><span style=\"color: #007700\">,&nbsp;<\/span><span style=\"color: #DD0000\">'content'&nbsp;<\/span><span style=\"color: #007700\">=&gt;&nbsp;<\/span><span style=\"color: #0000BB\">$userMessage1<\/span><span style=\"color: #007700\">]; <br \/>&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #0000BB\">$conversationHistory<\/span><span style=\"color: #007700\">[]&nbsp;=&nbsp;[<\/span><span style=\"color: #DD0000\">'role'&nbsp;<\/span><span style=\"color: #007700\">=&gt;&nbsp;<\/span><span style=\"color: #DD0000\">'assistant'<\/span><span style=\"color: #007700\">,&nbsp;<\/span><span style=\"color: #DD0000\">'content'&nbsp;<\/span><span style=\"color: #007700\">=&gt;&nbsp;<\/span><span style=\"color: #0000BB\">$response1<\/span><span style=\"color: #007700\">]; <br \/>} <br \/> <br \/><\/span><span style=\"color: #FF8000\">\/\/&nbsp;Follow-up&nbsp;message <br \/><\/span><span style=\"color: #0000BB\">$userMessage2&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;<\/span><span style=\"color: #DD0000\">\"What&nbsp;are&nbsp;the&nbsp;main&nbsp;types?\"<\/span><span style=\"color: #007700\">; <br \/><\/span><span style=\"color: #0000BB\">$response2&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;<\/span><span style=\"color: #0000BB\">$client<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">sendMessage<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">$userMessage2<\/span><span style=\"color: #007700\">,&nbsp;<\/span><span style=\"color: #0000BB\">$conversationHistory<\/span><span style=\"color: #007700\">); <br \/>if&nbsp;(<\/span><span style=\"color: #0000BB\">$response2<\/span><span style=\"color: #007700\">)&nbsp;{ <br \/>&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;<\/span><span style=\"color: #DD0000\">\"You:&nbsp;\"&nbsp;<\/span><span style=\"color: #007700\">.&nbsp;<\/span><span style=\"color: #0000BB\">$userMessage2&nbsp;<\/span><span style=\"color: #007700\">.&nbsp;<\/span><span style=\"color: #DD0000\">\"\\n\"<\/span><span style=\"color: #007700\">; <br \/>&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;<\/span><span style=\"color: #DD0000\">\"Bot:&nbsp;\"&nbsp;<\/span><span style=\"color: #007700\">.&nbsp;<\/span><span style=\"color: #0000BB\">$response2&nbsp;<\/span><span style=\"color: #007700\">.&nbsp;<\/span><span style=\"color: #DD0000\">\"\\n\\n\"<\/span><span style=\"color: #007700\">; <br \/>&nbsp;&nbsp;&nbsp;&nbsp; <br \/>&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #FF8000\">\/\/&nbsp;Add&nbsp;to&nbsp;history <br \/>&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #0000BB\">$conversationHistory<\/span><span style=\"color: #007700\">[]&nbsp;=&nbsp;[<\/span><span style=\"color: #DD0000\">'role'&nbsp;<\/span><span style=\"color: #007700\">=&gt;&nbsp;<\/span><span style=\"color: #DD0000\">'user'<\/span><span style=\"color: #007700\">,&nbsp;<\/span><span style=\"color: #DD0000\">'content'&nbsp;<\/span><span style=\"color: #007700\">=&gt;&nbsp;<\/span><span style=\"color: #0000BB\">$userMessage2<\/span><span style=\"color: #007700\">]; <br \/>&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #0000BB\">$conversationHistory<\/span><span style=\"color: #007700\">[]&nbsp;=&nbsp;[<\/span><span style=\"color: #DD0000\">'role'&nbsp;<\/span><span style=\"color: #007700\">=&gt;&nbsp;<\/span><span style=\"color: #DD0000\">'assistant'<\/span><span style=\"color: #007700\">,&nbsp;<\/span><span style=\"color: #DD0000\">'content'&nbsp;<\/span><span style=\"color: #007700\">=&gt;&nbsp;<\/span><span style=\"color: #0000BB\">$response2<\/span><span style=\"color: #007700\">]; <br \/>}<\/span><\/pre>\n<p><b>Example 3: With system prompt for role-playing<\/b><\/p>\n<pre><span style=\"color: #0000BB\">$systemPromptHistory&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;[ <br \/>&nbsp;&nbsp;&nbsp;&nbsp;[<\/span><span style=\"color: #DD0000\">'role'&nbsp;<\/span><span style=\"color: #007700\">=&gt;&nbsp;<\/span><span style=\"color: #DD0000\">'system'<\/span><span style=\"color: #007700\">,&nbsp;<\/span><span style=\"color: #DD0000\">'content'&nbsp;<\/span><span style=\"color: #007700\">=&gt;&nbsp;<\/span><span style=\"color: #DD0000\">'You&nbsp;are&nbsp;a&nbsp;helpful&nbsp;Python&nbsp;programming&nbsp;expert.'<\/span><span style=\"color: #007700\">] <br \/>]; <br \/> <br \/><\/span><span style=\"color: #0000BB\">$response3&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;<\/span><span style=\"color: #0000BB\">$client<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">sendMessage<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #DD0000\">'How&nbsp;do&nbsp;I&nbsp;read&nbsp;a&nbsp;file&nbsp;in&nbsp;Python?'<\/span><span style=\"color: #007700\">,&nbsp;<\/span><span style=\"color: #0000BB\">$systemPromptHistory<\/span><span style=\"color: #007700\">); <br \/>if&nbsp;(<\/span><span style=\"color: #0000BB\">$response3<\/span><span style=\"color: #007700\">)&nbsp;{ <br \/>&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;<\/span><span style=\"color: #DD0000\">\"You:&nbsp;How&nbsp;do&nbsp;I&nbsp;read&nbsp;a&nbsp;file&nbsp;in&nbsp;Python?\\n\"<\/span><span style=\"color: #007700\">; <br \/>&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;<\/span><span style=\"color: #DD0000\">\"Bot:&nbsp;\"&nbsp;<\/span><span style=\"color: #007700\">.&nbsp;<\/span><span style=\"color: #0000BB\">$response3&nbsp;<\/span><span style=\"color: #007700\">.&nbsp;<\/span><span style=\"color: #DD0000\">\"\\n\\n\"<\/span><span style=\"color: #007700\">; <br \/>}&nbsp;else&nbsp;{ <br \/>&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;<\/span><span style=\"color: #DD0000\">\"Error:&nbsp;\"&nbsp;<\/span><span style=\"color: #007700\">.&nbsp;<\/span><span style=\"color: #0000BB\">$client<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">getLastError<\/span><span style=\"color: #007700\">()&nbsp;.&nbsp;<\/span><span style=\"color: #DD0000\">\"\\n\\n\"<\/span><span style=\"color: #007700\">; <br \/>}<\/span><\/pre>\n<p><b>Example 4: Test connection<\/b><\/p>\n<pre><span style=\"color: #007700\">if&nbsp;(<\/span><span style=\"color: #0000BB\">$client<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">testConnection<\/span><span style=\"color: #007700\">())&nbsp;{ <br \/>&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;<\/span><span style=\"color: #DD0000\">\"Connection&nbsp;to&nbsp;OpenAI&nbsp;API:&nbsp;SUCCESS\\n\"<\/span><span style=\"color: #007700\">; <br \/>}&nbsp;else&nbsp;{ <br \/>&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;<\/span><span style=\"color: #DD0000\">\"Connection&nbsp;to&nbsp;OpenAI&nbsp;API:&nbsp;FAILED\\n\"<\/span><span style=\"color: #007700\">; <br \/>&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;<\/span><span style=\"color: #DD0000\">\"Error:&nbsp;\"&nbsp;<\/span><span style=\"color: #007700\">.&nbsp;<\/span><span style=\"color: #0000BB\">$client<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">getLastError<\/span><span style=\"color: #007700\">()&nbsp;.&nbsp;<\/span><span style=\"color: #DD0000\">\"\\n\"<\/span><span style=\"color: #007700\">; <br \/>}<\/span><\/pre>\n<h4>\ud83e\udde9 Now, let&#8217;s look at how the <code>api.php<\/code> file utilizes the <code>ChatGPTClient<\/code> class to handle AJAX requests from the frontend chat interface.<\/h4>\n<h2>Backend API Handler (api.php)<\/h2>\n<p>The <code>api.php<\/code> file serves as the backend endpoint that processes AJAX requests from the frontend chat interface. It performs the following tasks:<\/p>\n<ul>\n<li>Includes necessary files and initializes the ChatGPTClient.<\/li>\n<li>Validates incoming requests and user prompts.<\/li>\n<li>Handles conversation history to maintain context.<\/li>\n<li>Sends the user prompt to the ChatGPTClient and retrieves the response.<\/li>\n<li>Returns the AI response as a JSON object to the frontend.<\/li>\n<\/ul>\n<pre><span style=\"color: #0000BB\">&lt;?php <br \/>header<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #DD0000\">'Content-Type:&nbsp;application\/json'<\/span><span style=\"color: #007700\">); <br \/> <br \/>require_once&nbsp;<\/span><span style=\"color: #DD0000\">'config.php'<\/span><span style=\"color: #007700\">; <br \/>require_once&nbsp;<\/span><span style=\"color: #DD0000\">'ChatGPTClient.php'<\/span><span style=\"color: #007700\">; <br \/> <br \/><\/span><span style=\"color: #FF8000\">\/\/&nbsp;Check&nbsp;if&nbsp;API&nbsp;key&nbsp;is&nbsp;configured <br \/><\/span><span style=\"color: #007700\">if&nbsp;(empty(<\/span><span style=\"color: #0000BB\">OPENAI_API_KEY<\/span><span style=\"color: #007700\">)&nbsp;||&nbsp;<\/span><span style=\"color: #0000BB\">OPENAI_API_KEY&nbsp;<\/span><span style=\"color: #007700\">===&nbsp;<\/span><span style=\"color: #DD0000\">'your-api-key-here'<\/span><span style=\"color: #007700\">)&nbsp;{ <br \/>&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;<\/span><span style=\"color: #0000BB\">json_encode<\/span><span style=\"color: #007700\">([ <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #DD0000\">'success'&nbsp;<\/span><span style=\"color: #007700\">=&gt;&nbsp;<\/span><span style=\"color: #0000BB\">false<\/span><span style=\"color: #007700\">, <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #DD0000\">'error'&nbsp;<\/span><span style=\"color: #007700\">=&gt;&nbsp;<\/span><span style=\"color: #DD0000\">'API&nbsp;key&nbsp;is&nbsp;not&nbsp;configured.&nbsp;Please&nbsp;set&nbsp;your&nbsp;OpenAI&nbsp;API&nbsp;key&nbsp;in&nbsp;config.php' <br \/>&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #007700\">]); <br \/>&nbsp;&nbsp;&nbsp;&nbsp;exit; <br \/>} <br \/> <br \/>try&nbsp;{ <br \/>&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #0000BB\">$input&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;<\/span><span style=\"color: #0000BB\">json_decode<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">file_get_contents<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #DD0000\">'php:\/\/input'<\/span><span style=\"color: #007700\">),&nbsp;<\/span><span style=\"color: #0000BB\">true<\/span><span style=\"color: #007700\">); <br \/>&nbsp;&nbsp;&nbsp;&nbsp; <br \/>&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(!isset(<\/span><span style=\"color: #0000BB\">$input<\/span><span style=\"color: #007700\">[<\/span><span style=\"color: #DD0000\">'message'<\/span><span style=\"color: #007700\">]))&nbsp;{ <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;<\/span><span style=\"color: #0000BB\">json_encode<\/span><span style=\"color: #007700\">([ <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #DD0000\">'success'&nbsp;<\/span><span style=\"color: #007700\">=&gt;&nbsp;<\/span><span style=\"color: #0000BB\">false<\/span><span style=\"color: #007700\">, <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #DD0000\">'error'&nbsp;<\/span><span style=\"color: #007700\">=&gt;&nbsp;<\/span><span style=\"color: #DD0000\">'Message&nbsp;is&nbsp;required' <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #007700\">]); <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exit; <br \/>&nbsp;&nbsp;&nbsp;&nbsp;} <br \/> <br \/>&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #0000BB\">$message&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;<\/span><span style=\"color: #0000BB\">trim<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">$input<\/span><span style=\"color: #007700\">[<\/span><span style=\"color: #DD0000\">'message'<\/span><span style=\"color: #007700\">]); <br \/>&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(empty(<\/span><span style=\"color: #0000BB\">$message<\/span><span style=\"color: #007700\">))&nbsp;{ <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;<\/span><span style=\"color: #0000BB\">json_encode<\/span><span style=\"color: #007700\">([ <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #DD0000\">'success'&nbsp;<\/span><span style=\"color: #007700\">=&gt;&nbsp;<\/span><span style=\"color: #0000BB\">false<\/span><span style=\"color: #007700\">, <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #DD0000\">'error'&nbsp;<\/span><span style=\"color: #007700\">=&gt;&nbsp;<\/span><span style=\"color: #DD0000\">'Message&nbsp;cannot&nbsp;be&nbsp;empty' <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #007700\">]); <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exit; <br \/>&nbsp;&nbsp;&nbsp;&nbsp;} <br \/> <br \/>&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #FF8000\">\/\/&nbsp;Get&nbsp;conversation&nbsp;history&nbsp;if&nbsp;provided <br \/>&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #0000BB\">$history&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;isset(<\/span><span style=\"color: #0000BB\">$input<\/span><span style=\"color: #007700\">[<\/span><span style=\"color: #DD0000\">'history'<\/span><span style=\"color: #007700\">])&nbsp;?&nbsp;<\/span><span style=\"color: #0000BB\">$input<\/span><span style=\"color: #007700\">[<\/span><span style=\"color: #DD0000\">'history'<\/span><span style=\"color: #007700\">]&nbsp;:&nbsp;[]; <br \/> <br \/>&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #FF8000\">\/\/&nbsp;Initialize&nbsp;client <br \/>&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #0000BB\">$client&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;new&nbsp;<\/span><span style=\"color: #0000BB\">ChatGPTClient<\/span><span style=\"color: #007700\">( <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #0000BB\">OPENAI_API_KEY<\/span><span style=\"color: #007700\">, <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #0000BB\">OPENAI_MODEL<\/span><span style=\"color: #007700\">, <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #0000BB\">REQUEST_TIMEOUT<\/span><span style=\"color: #007700\">, <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #0000BB\">TEMPERATURE<\/span><span style=\"color: #007700\">, <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #0000BB\">MAX_TOKENS <br \/>&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #007700\">); <br \/> <br \/>&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #FF8000\">\/\/&nbsp;Send&nbsp;message <br \/>&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #0000BB\">$response&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;<\/span><span style=\"color: #0000BB\">$client<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">sendMessage<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">$message<\/span><span style=\"color: #007700\">,&nbsp;<\/span><span style=\"color: #0000BB\">$history<\/span><span style=\"color: #007700\">); <br \/> <br \/>&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(<\/span><span style=\"color: #0000BB\">$response&nbsp;<\/span><span style=\"color: #007700\">===&nbsp;<\/span><span style=\"color: #0000BB\">false<\/span><span style=\"color: #007700\">)&nbsp;{ <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;<\/span><span style=\"color: #0000BB\">json_encode<\/span><span style=\"color: #007700\">([ <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #DD0000\">'success'&nbsp;<\/span><span style=\"color: #007700\">=&gt;&nbsp;<\/span><span style=\"color: #0000BB\">false<\/span><span style=\"color: #007700\">, <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #DD0000\">'error'&nbsp;<\/span><span style=\"color: #007700\">=&gt;&nbsp;<\/span><span style=\"color: #0000BB\">$client<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">getLastError<\/span><span style=\"color: #007700\">() <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;]); <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exit; <br \/>&nbsp;&nbsp;&nbsp;&nbsp;} <br \/> <br \/>&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;<\/span><span style=\"color: #0000BB\">json_encode<\/span><span style=\"color: #007700\">([ <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #DD0000\">'success'&nbsp;<\/span><span style=\"color: #007700\">=&gt;&nbsp;<\/span><span style=\"color: #0000BB\">true<\/span><span style=\"color: #007700\">, <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #DD0000\">'response'&nbsp;<\/span><span style=\"color: #007700\">=&gt;&nbsp;<\/span><span style=\"color: #0000BB\">$response<\/span><span style=\"color: #007700\">, <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #DD0000\">'message'&nbsp;<\/span><span style=\"color: #007700\">=&gt;&nbsp;<\/span><span style=\"color: #0000BB\">$message <br \/>&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #007700\">]); <br \/> <br \/>}&nbsp;catch&nbsp;(<\/span><span style=\"color: #0000BB\">Exception&nbsp;$e<\/span><span style=\"color: #007700\">)&nbsp;{ <br \/>&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;<\/span><span style=\"color: #0000BB\">json_encode<\/span><span style=\"color: #007700\">([ <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #DD0000\">'success'&nbsp;<\/span><span style=\"color: #007700\">=&gt;&nbsp;<\/span><span style=\"color: #0000BB\">false<\/span><span style=\"color: #007700\">, <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #DD0000\">'error'&nbsp;<\/span><span style=\"color: #007700\">=&gt;&nbsp;<\/span><span style=\"color: #DD0000\">'Exception:&nbsp;'&nbsp;<\/span><span style=\"color: #007700\">.&nbsp;<\/span><span style=\"color: #0000BB\">$e<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">getMessage<\/span><span style=\"color: #007700\">() <br \/>&nbsp;&nbsp;&nbsp;&nbsp;]); <br \/>} <br \/><\/span><span style=\"color: #0000BB\">?&gt;<\/span><\/pre>\n<h2>Frontend Chat Interface (index.html)<\/h2>\n<p>The <code>index.html<\/code> file provides a simple and user-friendly chat interface where users can interact with the ChatGPT-powered chatbot. Key features include:<\/p>\n<ul>\n<li>A chat window displaying conversation history.<\/li>\n<li>An input field for users to type their messages.<\/li>\n<li>A send button to submit messages.<\/li>\n<li>AJAX functionality to send user messages to the backend without reloading the page.<\/li>\n<li>Dynamic updating of the chat window with user and AI messages.<\/li>\n<\/ul>\n<p><b>Chat Interface HTML:<\/b><br \/>\nDefine the chat interface structure with a chat window, input field, and buttons.<\/p>\n<pre style=\"color: rgb(95, 94, 78);\"><span class=\"hljs-tag\" style=\"color: rgb(186, 98, 54);\">&lt;<span class=\"hljs-name\" style=\"color: rgb(186, 98, 54);\">div<\/span> <span class=\"hljs-attr\">class<\/span>=<span class=\"hljs-string\" style=\"color: rgb(125, 151, 38);\">\"input-section\"<\/span>&gt;<\/span>\r\n    <span class=\"hljs-tag\" style=\"color: rgb(186, 98, 54);\">&lt;<span class=\"hljs-name\" style=\"color: rgb(186, 98, 54);\">div<\/span> <span class=\"hljs-attr\">class<\/span>=<span class=\"hljs-string\" style=\"color: rgb(125, 151, 38);\">\"input-wrapper\"<\/span>&gt;<\/span>\r\n        <span class=\"hljs-tag\" style=\"color: rgb(186, 98, 54);\">&lt;<span class=\"hljs-name\" style=\"color: rgb(186, 98, 54);\">input<\/span> \r\n            <span class=\"hljs-attr\">type<\/span>=<span class=\"hljs-string\" style=\"color: rgb(125, 151, 38);\">\"text\"<\/span> \r\n            <span class=\"hljs-attr\">id<\/span>=<span class=\"hljs-string\" style=\"color: rgb(125, 151, 38);\">\"messageInput\"<\/span> \r\n            <span class=\"hljs-attr\">placeholder<\/span>=<span class=\"hljs-string\" style=\"color: rgb(125, 151, 38);\">\"Type your message here...\"<\/span>\r\n            <span class=\"hljs-attr\">autocomplete<\/span>=<span class=\"hljs-string\" style=\"color: rgb(125, 151, 38);\">\"off\"<\/span>\r\n        &gt;<\/span>\r\n        <span class=\"hljs-tag\" style=\"color: rgb(186, 98, 54);\">&lt;<span class=\"hljs-name\" style=\"color: rgb(186, 98, 54);\">button<\/span> <span class=\"hljs-attr\">id<\/span>=<span class=\"hljs-string\" style=\"color: rgb(125, 151, 38);\">\"sendBtn\"<\/span> <span class=\"hljs-attr\">onclick<\/span>=<span class=\"hljs-string\" style=\"color: rgb(125, 151, 38);\">\"sendMessage()\"<\/span>&gt;<\/span>Send<span class=\"hljs-tag\" style=\"color: rgb(186, 98, 54);\">&lt;\/<span class=\"hljs-name\" style=\"color: rgb(186, 98, 54);\">button<\/span>&gt;<\/span>\r\n    <span class=\"hljs-tag\" style=\"color: rgb(186, 98, 54);\">&lt;\/<span class=\"hljs-name\" style=\"color: rgb(186, 98, 54);\">div<\/span>&gt;<\/span>\r\n    <span class=\"hljs-tag\" style=\"color: rgb(186, 98, 54);\">&lt;<span class=\"hljs-name\" style=\"color: rgb(186, 98, 54);\">button<\/span> <span class=\"hljs-attr\">class<\/span>=<span class=\"hljs-string\" style=\"color: rgb(125, 151, 38);\">\"clear-btn\"<\/span> <span class=\"hljs-attr\">onclick<\/span>=<span class=\"hljs-string\" style=\"color: rgb(125, 151, 38);\">\"clearChat()\"<\/span>&gt;<\/span>Clear<span class=\"hljs-tag\" style=\"color: rgb(186, 98, 54);\">&lt;\/<span class=\"hljs-name\" style=\"color: rgb(186, 98, 54);\">button<\/span>&gt;<\/span>\r\n<span class=\"hljs-tag\" style=\"color: rgb(186, 98, 54);\">&lt;\/<span class=\"hljs-name\" style=\"color: rgb(186, 98, 54);\">div<\/span>&gt;<\/span><\/pre>\n<p><b>AJAX Functionality (JavaScript):<\/b><br \/>\nCall the backend API (api.php) using AJAX to send user messages and receive AI responses.<\/p>\n<pre style=\"color: rgb(95, 94, 78);\"><span class=\"hljs-tag\" style=\"color: rgb(186, 98, 54);\">&lt;<span class=\"hljs-name\" style=\"color: rgb(186, 98, 54);\">script<\/span>&gt;<\/span>\r\n<span style=\"color: rgb(68, 68, 68);\">    <span class=\"hljs-keyword\" style=\"font-weight: 700;\">const<\/span> chatMessages = <span class=\"hljs-built_in\" style=\"color: rgb(57, 115, 0);\">document<\/span>.getElementById(<span class=\"hljs-string\" style=\"color: rgb(136, 0, 0);\">'chatMessages'<\/span>);\r\n    <span class=\"hljs-keyword\" style=\"font-weight: 700;\">const<\/span> messageInput = <span class=\"hljs-built_in\" style=\"color: rgb(57, 115, 0);\">document<\/span>.getElementById(<span class=\"hljs-string\" style=\"color: rgb(136, 0, 0);\">'messageInput'<\/span>);\r\n    <span class=\"hljs-keyword\" style=\"font-weight: 700;\">const<\/span> sendBtn = <span class=\"hljs-built_in\" style=\"color: rgb(57, 115, 0);\">document<\/span>.getElementById(<span class=\"hljs-string\" style=\"color: rgb(136, 0, 0);\">'sendBtn'<\/span>);\r\n    <span class=\"hljs-keyword\" style=\"font-weight: 700;\">let<\/span> conversationHistory = [];\r\n    <span class=\"hljs-keyword\" style=\"font-weight: 700;\">let<\/span> isLoading = <span class=\"hljs-literal\" style=\"color: rgb(120, 169, 96);\">false<\/span>;\r\n\r\n    <span class=\"hljs-comment\" style=\"color: rgb(136, 136, 136);\">\/\/ Send message on Enter key<\/span>\r\n    messageInput.addEventListener(<span class=\"hljs-string\" style=\"color: rgb(136, 0, 0);\">'keypress'<\/span>, <span class=\"hljs-function\"><span class=\"hljs-keyword\" style=\"font-weight: 700;\">function<\/span>(<span class=\"hljs-params\">e<\/span>) <\/span>{\r\n        <span class=\"hljs-keyword\" style=\"font-weight: 700;\">if<\/span> (e.key === <span class=\"hljs-string\" style=\"color: rgb(136, 0, 0);\">'Enter'<\/span> &amp;&amp; !isLoading) {\r\n            sendMessage();\r\n        }\r\n    });\r\n\r\n    <span class=\"hljs-keyword\" style=\"font-weight: 700;\">async<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\" style=\"font-weight: 700;\">function<\/span> <span class=\"hljs-title\" style=\"color: rgb(136, 0, 0); font-weight: 700;\">sendMessage<\/span>(<span class=\"hljs-params\"><\/span>) <\/span>{\r\n        <span class=\"hljs-keyword\" style=\"font-weight: 700;\">const<\/span> message = messageInput.value.trim();\r\n        \r\n        <span class=\"hljs-keyword\" style=\"font-weight: 700;\">if<\/span> (!message) <span class=\"hljs-keyword\" style=\"font-weight: 700;\">return<\/span>;\r\n        <span class=\"hljs-keyword\" style=\"font-weight: 700;\">if<\/span> (isLoading) <span class=\"hljs-keyword\" style=\"font-weight: 700;\">return<\/span>;\r\n\r\n        isLoading = <span class=\"hljs-literal\" style=\"color: rgb(120, 169, 96);\">true<\/span>;\r\n        sendBtn.disabled = <span class=\"hljs-literal\" style=\"color: rgb(120, 169, 96);\">true<\/span>;\r\n        messageInput.disabled = <span class=\"hljs-literal\" style=\"color: rgb(120, 169, 96);\">true<\/span>;\r\n\r\n        <span class=\"hljs-comment\" style=\"color: rgb(136, 136, 136);\">\/\/ Add user message to chat<\/span>\r\n        addMessage(message, <span class=\"hljs-string\" style=\"color: rgb(136, 0, 0);\">'user'<\/span>);\r\n        messageInput.value = <span class=\"hljs-string\" style=\"color: rgb(136, 0, 0);\">''<\/span>;\r\n\r\n        <span class=\"hljs-comment\" style=\"color: rgb(136, 136, 136);\">\/\/ Show loading indicator<\/span>\r\n        <span class=\"hljs-keyword\" style=\"font-weight: 700;\">const<\/span> loadingElement = showLoadingIndicator();\r\n\r\n        <span class=\"hljs-keyword\" style=\"font-weight: 700;\">try<\/span> {\r\n            <span class=\"hljs-keyword\" style=\"font-weight: 700;\">const<\/span> response = <span class=\"hljs-keyword\" style=\"font-weight: 700;\">await<\/span> fetch(<span class=\"hljs-string\" style=\"color: rgb(136, 0, 0);\">'api.php'<\/span>, {\r\n                method: <span class=\"hljs-string\" style=\"color: rgb(136, 0, 0);\">'POST'<\/span>,\r\n                headers: {\r\n                    <span class=\"hljs-string\" style=\"color: rgb(136, 0, 0);\">'Content-Type'<\/span>: <span class=\"hljs-string\" style=\"color: rgb(136, 0, 0);\">'application\/json'<\/span>,\r\n                },\r\n                body: <span class=\"hljs-built_in\" style=\"color: rgb(57, 115, 0);\">JSON<\/span>.stringify({\r\n                    message: message,\r\n                    history: conversationHistory\r\n                })\r\n            });\r\n\r\n            <span class=\"hljs-keyword\" style=\"font-weight: 700;\">const<\/span> data = <span class=\"hljs-keyword\" style=\"font-weight: 700;\">await<\/span> response.json();\r\n\r\n            <span class=\"hljs-comment\" style=\"color: rgb(136, 136, 136);\">\/\/ Remove loading indicator<\/span>\r\n            loadingElement.remove();\r\n\r\n            <span class=\"hljs-keyword\" style=\"font-weight: 700;\">if<\/span> (data.success) {\r\n                <span class=\"hljs-comment\" style=\"color: rgb(136, 136, 136);\">\/\/ Add bot response to chat<\/span>\r\n                addMessage(data.response, <span class=\"hljs-string\" style=\"color: rgb(136, 0, 0);\">'assistant'<\/span>);\r\n\r\n                <span class=\"hljs-comment\" style=\"color: rgb(136, 136, 136);\">\/\/ Update conversation history<\/span>\r\n                conversationHistory.push({\r\n                    role: <span class=\"hljs-string\" style=\"color: rgb(136, 0, 0);\">'user'<\/span>,\r\n                    content: message\r\n                });\r\n                conversationHistory.push({\r\n                    role: <span class=\"hljs-string\" style=\"color: rgb(136, 0, 0);\">'assistant'<\/span>,\r\n                    content: data.response\r\n                });\r\n            } <span class=\"hljs-keyword\" style=\"font-weight: 700;\">else<\/span> {\r\n                addMessage(<span class=\"hljs-string\" style=\"color: rgb(136, 0, 0);\">'Error: '<\/span> + data.error, <span class=\"hljs-string\" style=\"color: rgb(136, 0, 0);\">'error'<\/span>);\r\n            }\r\n        } <span class=\"hljs-keyword\" style=\"font-weight: 700;\">catch<\/span> (error) {\r\n            loadingElement.remove();\r\n            addMessage(<span class=\"hljs-string\" style=\"color: rgb(136, 0, 0);\">'Network error: '<\/span> + error.message, <span class=\"hljs-string\" style=\"color: rgb(136, 0, 0);\">'error'<\/span>);\r\n        } <span class=\"hljs-keyword\" style=\"font-weight: 700;\">finally<\/span> {\r\n            isLoading = <span class=\"hljs-literal\" style=\"color: rgb(120, 169, 96);\">false<\/span>;\r\n            sendBtn.disabled = <span class=\"hljs-literal\" style=\"color: rgb(120, 169, 96);\">false<\/span>;\r\n            messageInput.disabled = <span class=\"hljs-literal\" style=\"color: rgb(120, 169, 96);\">false<\/span>;\r\n            messageInput.focus();\r\n        }\r\n    }\r\n\r\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\" style=\"font-weight: 700;\">function<\/span> <span class=\"hljs-title\" style=\"color: rgb(136, 0, 0); font-weight: 700;\">addMessage<\/span>(<span class=\"hljs-params\">text, role<\/span>) <\/span>{\r\n        <span class=\"hljs-keyword\" style=\"font-weight: 700;\">const<\/span> messageDiv = <span class=\"hljs-built_in\" style=\"color: rgb(57, 115, 0);\">document<\/span>.createElement(<span class=\"hljs-string\" style=\"color: rgb(136, 0, 0);\">'div'<\/span>);\r\n        messageDiv.className = <span class=\"hljs-string\" style=\"color: rgb(136, 0, 0);\">'message '<\/span> + role;\r\n\r\n        <span class=\"hljs-keyword\" style=\"font-weight: 700;\">const<\/span> contentDiv = <span class=\"hljs-built_in\" style=\"color: rgb(57, 115, 0);\">document<\/span>.createElement(<span class=\"hljs-string\" style=\"color: rgb(136, 0, 0);\">'div'<\/span>);\r\n        contentDiv.className = <span class=\"hljs-string\" style=\"color: rgb(136, 0, 0);\">'message-content'<\/span>;\r\n        contentDiv.innerHTML = escapeHtml(text) + \r\n            <span class=\"hljs-string\" style=\"color: rgb(136, 0, 0);\">'&lt;div class=\"timestamp\"&gt;'<\/span> + <span class=\"hljs-keyword\" style=\"font-weight: 700;\">new<\/span> <span class=\"hljs-built_in\" style=\"color: rgb(57, 115, 0);\">Date<\/span>().toLocaleTimeString() + <span class=\"hljs-string\" style=\"color: rgb(136, 0, 0);\">'&lt;\/div&gt;'<\/span>;\r\n\r\n        messageDiv.appendChild(contentDiv);\r\n        chatMessages.appendChild(messageDiv);\r\n\r\n        <span class=\"hljs-comment\" style=\"color: rgb(136, 136, 136);\">\/\/ Auto scroll to bottom<\/span>\r\n        chatMessages.scrollTop = chatMessages.scrollHeight;\r\n    }\r\n\r\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\" style=\"font-weight: 700;\">function<\/span> <span class=\"hljs-title\" style=\"color: rgb(136, 0, 0); font-weight: 700;\">showLoadingIndicator<\/span>(<span class=\"hljs-params\"><\/span>) <\/span>{\r\n        <span class=\"hljs-keyword\" style=\"font-weight: 700;\">const<\/span> messageDiv = <span class=\"hljs-built_in\" style=\"color: rgb(57, 115, 0);\">document<\/span>.createElement(<span class=\"hljs-string\" style=\"color: rgb(136, 0, 0);\">'div'<\/span>);\r\n        messageDiv.className = <span class=\"hljs-string\" style=\"color: rgb(136, 0, 0);\">'message assistant'<\/span>;\r\n\r\n        <span class=\"hljs-keyword\" style=\"font-weight: 700;\">const<\/span> contentDiv = <span class=\"hljs-built_in\" style=\"color: rgb(57, 115, 0);\">document<\/span>.createElement(<span class=\"hljs-string\" style=\"color: rgb(136, 0, 0);\">'div'<\/span>);\r\n        contentDiv.className = <span class=\"hljs-string\" style=\"color: rgb(136, 0, 0);\">'message-content'<\/span>;\r\n        contentDiv.innerHTML = <span class=\"hljs-string\" style=\"color: rgb(136, 0, 0);\">'&lt;div class=\"loading\"&gt;&lt;span&gt;&lt;\/span&gt;&lt;span&gt;&lt;\/span&gt;&lt;span&gt;&lt;\/span&gt;&lt;\/div&gt;'<\/span>;\r\n\r\n        messageDiv.appendChild(contentDiv);\r\n        chatMessages.appendChild(messageDiv);\r\n\r\n        <span class=\"hljs-comment\" style=\"color: rgb(136, 136, 136);\">\/\/ Auto scroll to bottom<\/span>\r\n        chatMessages.scrollTop = chatMessages.scrollHeight;\r\n\r\n        <span class=\"hljs-keyword\" style=\"font-weight: 700;\">return<\/span> messageDiv;\r\n    }\r\n\r\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\" style=\"font-weight: 700;\">function<\/span> <span class=\"hljs-title\" style=\"color: rgb(136, 0, 0); font-weight: 700;\">clearChat<\/span>(<span class=\"hljs-params\"><\/span>) <\/span>{\r\n        <span class=\"hljs-keyword\" style=\"font-weight: 700;\">if<\/span> (confirm(<span class=\"hljs-string\" style=\"color: rgb(136, 0, 0);\">'Are you sure you want to clear the chat?'<\/span>)) {\r\n            chatMessages.innerHTML = <span class=\"hljs-string\" style=\"color: rgb(136, 0, 0);\">'&lt;div class=\"message system\"&gt;&lt;div class=\"message-content\"&gt;Chat cleared. Start a new conversation!&lt;\/div&gt;&lt;\/div&gt;'<\/span>;\r\n            conversationHistory = [];\r\n            messageInput.focus();\r\n        }\r\n    }\r\n\r\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\" style=\"font-weight: 700;\">function<\/span> <span class=\"hljs-title\" style=\"color: rgb(136, 0, 0); font-weight: 700;\">escapeHtml<\/span>(<span class=\"hljs-params\">text<\/span>) <\/span>{\r\n        <span class=\"hljs-keyword\" style=\"font-weight: 700;\">const<\/span> div = <span class=\"hljs-built_in\" style=\"color: rgb(57, 115, 0);\">document<\/span>.createElement(<span class=\"hljs-string\" style=\"color: rgb(136, 0, 0);\">'div'<\/span>);\r\n        div.textContent = text;\r\n        <span class=\"hljs-keyword\" style=\"font-weight: 700;\">return<\/span> div.innerHTML;\r\n    }\r\n\r\n    <span class=\"hljs-comment\" style=\"color: rgb(136, 136, 136);\">\/\/ Focus input on load<\/span>\r\n    messageInput.focus();<\/span>\r\n<span class=\"hljs-tag\" style=\"color: rgb(186, 98, 54);\">&lt;\/<span class=\"hljs-name\" style=\"color: rgb(186, 98, 54);\">script<\/span>&gt;<\/span><\/pre>\n<h2>\ud83d\udd10 Security &#038; Best Practices<\/h2>\n<ul>\n<li>Store API keys securely<\/li>\n<li>Use environment variables in production<\/li>\n<li>Limit MAX_TOKENS to control costs<\/li>\n<li>Cache responses where possible<\/li>\n<li>Log errors for monitoring<\/li>\n<li>Sanitize user input<\/li>\n<\/ul>\n<h2>\ud83c\udfaf Use Cases of ChatGPT API in PHP<\/h2>\n<p>This ChatGPT PHP integration can be utilized in various applications, including:<\/p>\n<ul>\n<li>AI chatbots for websites<\/li>\n<li>Customer support systems<\/li>\n<li>Virtual assistants<\/li>\n<li>Content generation tools<\/li>\n<li>Educational platforms<\/li>\n<li>SaaS AI features<\/li>\n<li>Interactive storytelling applications<\/li>\n<\/ul>\n<h2>\ud83d\ude80 Conclusion<\/h2>\n<p>You have successfully learned <b>how to integrate the ChatGPT API in PHP<\/b> using a clean, scalable, and production-ready approach. This implementation is ideal for real-world applications and can be extended with authentication, database storage, or advanced prompt engineering. If you are building modern PHP applications, ChatGPT integration is a powerful way to enhance user experience and automation.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The ChatGPT API, provided by OpenAI, enables developers to access the powerful language model capabilities of ChatGPT. By integrating this API into your PHP applications, you can create interactive and intelligent conversational agents that can <\/p>\n","protected":false},"author":1,"featured_media":6047,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[4],"tags":[250,395,396,14],"class_list":["post-6036","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-php","tag-api","tag-automation","tag-chatgpt","tag-php","cat-4-id","has_thumb"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>ChatGPT API Integration in PHP - CodexWorld<\/title>\n<meta name=\"description\" content=\"Learn how to integrate the ChatGPT API in PHP with a complete step-by-step tutorial. Includes OpenAI API setup, PHP backend, AJAX chat UI, conversation handling, and best practices.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.codexworld.com\/chatgpt-api-integration-in-php\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"ChatGPT API Integration in PHP - CodexWorld\" \/>\n<meta property=\"og:description\" content=\"Learn how to integrate the ChatGPT API in PHP with a complete step-by-step tutorial. Includes OpenAI API setup, PHP backend, AJAX chat UI, conversation handling, and best practices.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.codexworld.com\/chatgpt-api-integration-in-php\/\" \/>\n<meta property=\"og:site_name\" content=\"CodexWorld\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/codexworld\" \/>\n<meta property=\"article:author\" content=\"https:\/\/www.facebook.com\/codexworld\" \/>\n<meta property=\"article:published_time\" content=\"2026-01-22T09:12:10+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-01-22T09:20:11+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.codexworld.com\/wp-content\/uploads\/2026\/01\/chatgpt-api-integration-in-php-codexworld.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1366\" \/>\n\t<meta property=\"og:image:height\" content=\"768\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"CodexWorld\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@codexworldblog\" \/>\n<meta name=\"twitter:site\" content=\"@codexworldweb\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"CodexWorld\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.codexworld.com\\\/chatgpt-api-integration-in-php\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.codexworld.com\\\/chatgpt-api-integration-in-php\\\/\"},\"author\":{\"name\":\"CodexWorld\",\"@id\":\"https:\\\/\\\/www.codexworld.com\\\/#\\\/schema\\\/person\\\/9da51d8fa3cdefeb5ec9c69136d4baf0\"},\"headline\":\"ChatGPT API Integration in PHP\",\"datePublished\":\"2026-01-22T09:12:10+00:00\",\"dateModified\":\"2026-01-22T09:20:11+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.codexworld.com\\\/chatgpt-api-integration-in-php\\\/\"},\"wordCount\":900,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/www.codexworld.com\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/www.codexworld.com\\\/chatgpt-api-integration-in-php\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.codexworld.com\\\/wp-content\\\/uploads\\\/2026\\\/01\\\/chatgpt-api-integration-in-php-codexworld.jpg\",\"keywords\":[\"API\",\"Automation\",\"ChatGPT\",\"PHP\"],\"articleSection\":[\"PHP\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.codexworld.com\\\/chatgpt-api-integration-in-php\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.codexworld.com\\\/chatgpt-api-integration-in-php\\\/\",\"url\":\"https:\\\/\\\/www.codexworld.com\\\/chatgpt-api-integration-in-php\\\/\",\"name\":\"ChatGPT API Integration in PHP - CodexWorld\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.codexworld.com\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.codexworld.com\\\/chatgpt-api-integration-in-php\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.codexworld.com\\\/chatgpt-api-integration-in-php\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.codexworld.com\\\/wp-content\\\/uploads\\\/2026\\\/01\\\/chatgpt-api-integration-in-php-codexworld.jpg\",\"datePublished\":\"2026-01-22T09:12:10+00:00\",\"dateModified\":\"2026-01-22T09:20:11+00:00\",\"description\":\"Learn how to integrate the ChatGPT API in PHP with a complete step-by-step tutorial. Includes OpenAI API setup, PHP backend, AJAX chat UI, conversation handling, and best practices.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.codexworld.com\\\/chatgpt-api-integration-in-php\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.codexworld.com\\\/chatgpt-api-integration-in-php\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.codexworld.com\\\/chatgpt-api-integration-in-php\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.codexworld.com\\\/wp-content\\\/uploads\\\/2026\\\/01\\\/chatgpt-api-integration-in-php-codexworld.jpg\",\"contentUrl\":\"https:\\\/\\\/www.codexworld.com\\\/wp-content\\\/uploads\\\/2026\\\/01\\\/chatgpt-api-integration-in-php-codexworld.jpg\",\"width\":1366,\"height\":768,\"caption\":\"chatgpt-api-integration-in-php-codexworld\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.codexworld.com\\\/chatgpt-api-integration-in-php\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.codexworld.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"ChatGPT API Integration in PHP\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.codexworld.com\\\/#website\",\"url\":\"https:\\\/\\\/www.codexworld.com\\\/\",\"name\":\"CodexWorld\",\"description\":\"Web &amp; Mobile App Development Company\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.codexworld.com\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.codexworld.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.codexworld.com\\\/#organization\",\"name\":\"CodexWorld\",\"url\":\"https:\\\/\\\/www.codexworld.com\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.codexworld.com\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/www.codexworld.com\\\/wp-content\\\/uploads\\\/2014\\\/09\\\/codexworld-logo.png\",\"contentUrl\":\"https:\\\/\\\/www.codexworld.com\\\/wp-content\\\/uploads\\\/2014\\\/09\\\/codexworld-logo.png\",\"width\":200,\"height\":19,\"caption\":\"CodexWorld\"},\"image\":{\"@id\":\"https:\\\/\\\/www.codexworld.com\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/codexworld\",\"https:\\\/\\\/x.com\\\/codexworldweb\",\"https:\\\/\\\/www.linkedin.com\\\/company\\\/codexworld\",\"https:\\\/\\\/www.youtube.com\\\/codexworld\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.codexworld.com\\\/#\\\/schema\\\/person\\\/9da51d8fa3cdefeb5ec9c69136d4baf0\",\"name\":\"CodexWorld\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/cf4999db3b409de559f80677afa01729bb2eeda19be273c254e8b2c22729e386?s=96&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/cf4999db3b409de559f80677afa01729bb2eeda19be273c254e8b2c22729e386?s=96&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/cf4999db3b409de559f80677afa01729bb2eeda19be273c254e8b2c22729e386?s=96&r=g\",\"caption\":\"CodexWorld\"},\"description\":\"CodexWorld is a programming blog, one-stop destination for web professionals \u2014 developers, programmers, freelancers, and site owners.\",\"sameAs\":[\"http:\\\/\\\/www.codexworld.com\",\"https:\\\/\\\/www.facebook.com\\\/codexworld\",\"https:\\\/\\\/x.com\\\/codexworldblog\"],\"url\":\"https:\\\/\\\/www.codexworld.com\\\/author\\\/nitya192265\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"ChatGPT API Integration in PHP - CodexWorld","description":"Learn how to integrate the ChatGPT API in PHP with a complete step-by-step tutorial. Includes OpenAI API setup, PHP backend, AJAX chat UI, conversation handling, and best practices.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.codexworld.com\/chatgpt-api-integration-in-php\/","og_locale":"en_US","og_type":"article","og_title":"ChatGPT API Integration in PHP - CodexWorld","og_description":"Learn how to integrate the ChatGPT API in PHP with a complete step-by-step tutorial. Includes OpenAI API setup, PHP backend, AJAX chat UI, conversation handling, and best practices.","og_url":"https:\/\/www.codexworld.com\/chatgpt-api-integration-in-php\/","og_site_name":"CodexWorld","article_publisher":"https:\/\/www.facebook.com\/codexworld","article_author":"https:\/\/www.facebook.com\/codexworld","article_published_time":"2026-01-22T09:12:10+00:00","article_modified_time":"2026-01-22T09:20:11+00:00","og_image":[{"width":1366,"height":768,"url":"https:\/\/www.codexworld.com\/wp-content\/uploads\/2026\/01\/chatgpt-api-integration-in-php-codexworld.jpg","type":"image\/jpeg"}],"author":"CodexWorld","twitter_card":"summary_large_image","twitter_creator":"@codexworldblog","twitter_site":"@codexworldweb","twitter_misc":{"Written by":"CodexWorld","Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.codexworld.com\/chatgpt-api-integration-in-php\/#article","isPartOf":{"@id":"https:\/\/www.codexworld.com\/chatgpt-api-integration-in-php\/"},"author":{"name":"CodexWorld","@id":"https:\/\/www.codexworld.com\/#\/schema\/person\/9da51d8fa3cdefeb5ec9c69136d4baf0"},"headline":"ChatGPT API Integration in PHP","datePublished":"2026-01-22T09:12:10+00:00","dateModified":"2026-01-22T09:20:11+00:00","mainEntityOfPage":{"@id":"https:\/\/www.codexworld.com\/chatgpt-api-integration-in-php\/"},"wordCount":900,"commentCount":0,"publisher":{"@id":"https:\/\/www.codexworld.com\/#organization"},"image":{"@id":"https:\/\/www.codexworld.com\/chatgpt-api-integration-in-php\/#primaryimage"},"thumbnailUrl":"https:\/\/www.codexworld.com\/wp-content\/uploads\/2026\/01\/chatgpt-api-integration-in-php-codexworld.jpg","keywords":["API","Automation","ChatGPT","PHP"],"articleSection":["PHP"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.codexworld.com\/chatgpt-api-integration-in-php\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.codexworld.com\/chatgpt-api-integration-in-php\/","url":"https:\/\/www.codexworld.com\/chatgpt-api-integration-in-php\/","name":"ChatGPT API Integration in PHP - CodexWorld","isPartOf":{"@id":"https:\/\/www.codexworld.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.codexworld.com\/chatgpt-api-integration-in-php\/#primaryimage"},"image":{"@id":"https:\/\/www.codexworld.com\/chatgpt-api-integration-in-php\/#primaryimage"},"thumbnailUrl":"https:\/\/www.codexworld.com\/wp-content\/uploads\/2026\/01\/chatgpt-api-integration-in-php-codexworld.jpg","datePublished":"2026-01-22T09:12:10+00:00","dateModified":"2026-01-22T09:20:11+00:00","description":"Learn how to integrate the ChatGPT API in PHP with a complete step-by-step tutorial. Includes OpenAI API setup, PHP backend, AJAX chat UI, conversation handling, and best practices.","breadcrumb":{"@id":"https:\/\/www.codexworld.com\/chatgpt-api-integration-in-php\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.codexworld.com\/chatgpt-api-integration-in-php\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.codexworld.com\/chatgpt-api-integration-in-php\/#primaryimage","url":"https:\/\/www.codexworld.com\/wp-content\/uploads\/2026\/01\/chatgpt-api-integration-in-php-codexworld.jpg","contentUrl":"https:\/\/www.codexworld.com\/wp-content\/uploads\/2026\/01\/chatgpt-api-integration-in-php-codexworld.jpg","width":1366,"height":768,"caption":"chatgpt-api-integration-in-php-codexworld"},{"@type":"BreadcrumbList","@id":"https:\/\/www.codexworld.com\/chatgpt-api-integration-in-php\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.codexworld.com\/"},{"@type":"ListItem","position":2,"name":"ChatGPT API Integration in PHP"}]},{"@type":"WebSite","@id":"https:\/\/www.codexworld.com\/#website","url":"https:\/\/www.codexworld.com\/","name":"CodexWorld","description":"Web &amp; Mobile App Development Company","publisher":{"@id":"https:\/\/www.codexworld.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.codexworld.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.codexworld.com\/#organization","name":"CodexWorld","url":"https:\/\/www.codexworld.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.codexworld.com\/#\/schema\/logo\/image\/","url":"https:\/\/www.codexworld.com\/wp-content\/uploads\/2014\/09\/codexworld-logo.png","contentUrl":"https:\/\/www.codexworld.com\/wp-content\/uploads\/2014\/09\/codexworld-logo.png","width":200,"height":19,"caption":"CodexWorld"},"image":{"@id":"https:\/\/www.codexworld.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/codexworld","https:\/\/x.com\/codexworldweb","https:\/\/www.linkedin.com\/company\/codexworld","https:\/\/www.youtube.com\/codexworld"]},{"@type":"Person","@id":"https:\/\/www.codexworld.com\/#\/schema\/person\/9da51d8fa3cdefeb5ec9c69136d4baf0","name":"CodexWorld","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/cf4999db3b409de559f80677afa01729bb2eeda19be273c254e8b2c22729e386?s=96&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/cf4999db3b409de559f80677afa01729bb2eeda19be273c254e8b2c22729e386?s=96&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/cf4999db3b409de559f80677afa01729bb2eeda19be273c254e8b2c22729e386?s=96&r=g","caption":"CodexWorld"},"description":"CodexWorld is a programming blog, one-stop destination for web professionals \u2014 developers, programmers, freelancers, and site owners.","sameAs":["http:\/\/www.codexworld.com","https:\/\/www.facebook.com\/codexworld","https:\/\/x.com\/codexworldblog"],"url":"https:\/\/www.codexworld.com\/author\/nitya192265\/"}]}},"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/www.codexworld.com\/wp-content\/uploads\/2026\/01\/chatgpt-api-integration-in-php-codexworld.jpg","jetpack_shortlink":"https:\/\/wp.me\/p6bxIh-1zm","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.codexworld.com\/wp-json\/wp\/v2\/posts\/6036","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.codexworld.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.codexworld.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.codexworld.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.codexworld.com\/wp-json\/wp\/v2\/comments?post=6036"}],"version-history":[{"count":8,"href":"https:\/\/www.codexworld.com\/wp-json\/wp\/v2\/posts\/6036\/revisions"}],"predecessor-version":[{"id":6045,"href":"https:\/\/www.codexworld.com\/wp-json\/wp\/v2\/posts\/6036\/revisions\/6045"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.codexworld.com\/wp-json\/wp\/v2\/media\/6047"}],"wp:attachment":[{"href":"https:\/\/www.codexworld.com\/wp-json\/wp\/v2\/media?parent=6036"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.codexworld.com\/wp-json\/wp\/v2\/categories?post=6036"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.codexworld.com\/wp-json\/wp\/v2\/tags?post=6036"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}