Are you interested in integrating the power of OpenAI’s ChatGPT into your PHP applications? In this step-by-step guide, we’ll walk you through the process of using the ChatGPT API with PHP and CURL. It’s easier than you might think!
Prerequisites
Before we get started, make sure you have the following prerequisites in place:
- OpenAI Account: You need an account on the OpenAI platform. If you don’t have one, you can sign up at OpenAI’s website.
- API Key: Once you have an account, you’ll need an API key. You can find this key in your OpenAI account settings or you can try this direct link once signup: https://platform.openai.com/account/api-keys
- PHP Installed: Ensure you have PHP installed on your computer or server. You can check if PHP is installed by runningin your terminal.php -v
- CURL Extension: You also need the CURL extension for PHP. Most PHP installations come with CURL pre-installed, but you can verify it by runningit in your terminal. If you don’t see anything, you might need to install CURL.php -m | grep curl
Now that you have everything ready, let’s proceed with the steps.
Step 1: Set Up Your PHP Environment
First, create a new PHP file “chatgpt.php” or whatever you want to give it in your project directory, and include the following code at the beginning to initialize CURL:
1 2 3 4 5 | <?php $apiKey = 'YOUR_API_KEY'; // Replace with your actual API key $url = 'https://api.openai.com/v1/chat/completions'; // API endpoint $auth_token = 'Bearer '.$apiKey; ?> |
Step 2: Prepare Your Request Data
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | <?php //Get the dynamic data from your HTML form $question = $this->input->post('term'); $limit = intval($this->input->post('term_size')); //by default it will consider post input as a string so need to convert it to number, otherwise API throw error. $data = array( "model" => "gpt-3.5-turbo", "messages" => array( array( "role" => "user", "content" => $question, ) ), "max_tokens" => $limit ); $payload = json_encode($data); $headers = array( 'Content-Type: application/json', 'Authorization: ' . $auth_token, 'Content-Length: ' . strlen($payload) ); $ch = curl_init($url); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); curl_setopt($ch, CURLOPT_POSTFIELDS, $payload); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); $result = curl_exec($ch); curl_close($ch); $response = json_decode($result, true); if (!empty($response['choices'][0]['message']['content'])) { $answer = $response['choices'][0]['message']['content']; echo $answer; } else { echo 'error'; //To get the specific error use response error echo json_encode($response['error']); } ?> |
That’s it! You’ve successfully used the ChatGPT API in PHP with CURL. You can customize your prompts and responses as needed for your specific use case.
Remember to handle errors and exceptions gracefully in your production code, and you’re ready to enhance your applications with the power of ChatGPT.
Important Note
ChatGPI API is not free, but users do get a free credit of about $18 when they create an account on OpenAPI. For this, visit the link of OpenAI API Key and log in.