ChatGPT API Integration with PHP


Noah Wilson

Published: Jan. 3rd, 2024


Introduction

Welcome to this comprehensive guide on integrating ChatGPT API with PHP! In this step-by-step tutorial, I will walk you through the process of seamlessly incorporating OpenAI's ChatGPT into your PHP applications.

Whether you're a developer looking to enhance user interactions on your website or a business owner aiming to provide real-time assistance, this guide is your roadmap to harnessing the capabilities of ChatGPT with the simplicity of PHP. Let's dive in and unlock the potential of AI-driven conversations.

Creating an OpenAI Account

Before you can start making API calls on your website, you need to obtain an API key from OpenAI by creating an account. Once you have an OpenAI account and an API key, you will be ready to make API calls on your website.

Setting up the PHP Class

We will be interacting with OpenAI using PHP's cURL library. To ensure reusability, we will construct a PHP class that encapsulates all the necessary logic for making calls to ChatGPT.

Inside the class constructor, we need to create two variables. Set the `$this->endpoint` variable to `https://api.openai.com/v1/chat/completions` and the `$this->apikey` variable to your API key.

class openAIPHP {
	public function __construct(){
		$this->endpoint = "https://api.openai.com/v1/chat/completions";
		$this->apikey = "Authorization: Bearer --YOUR--API--KEY--HERE--";
	}

Next, let's create a method that calls the OpenAI API, taking user input as a parameter.

          public function call($request){
		$ch = curl_init();
		
		$post = '{
			"model": "gpt-3.5-turbo",
			"messages": [{"role":"user", "content": "'.$request.'"}],
			"max_tokens": 512,
			"top_p": 1,
			"temperature": 0.5,
			"frequency_penalty": 0,
			"presence_penalty": 0
		}';

		curl_setopt($ch, CURLOPT_URL, $this->endpoint);
		curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json' , $this->apikey ));
        	curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
        	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        	curl_setopt($ch, CURLOPT_POSTFIELDS,$post);
        	curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);

		if(($result = curl_exec($ch)) === false){
            		$result = curl_error($ch);
        	}
        	curl_close($ch);
        	return $result;
	}
 }

Making the API Call

Now that our PHP class is properly configured, it's time to use the API. Create an instance of the class we just created and execute the 'call' method. Don't forget to pass in some user input. You can customize your ChatGPT Agent from within the `$post` variable of the 'call' method. Follow this guide for fine tuning assistance..

Feel free to replace `OpenAIPHP` with your preferred class name and update the API endpoint and key accordingly. This class will allow you to easily integrate ChatGPT into your PHP application.

Conclusion

In closing, we've embarked on a journey to bridge the gap between human communication and artificial intelligence by integrating ChatGPT with PHP. Through the meticulous steps outlined in this guide, you've learned how to create an OpenAI account, set up a PHP class for API interactions, and execute API calls seamlessly.

The possibilities are endless – from crafting responsive chatbots to enhancing customer support systems and beyond. As technology continues to evolve, your newfound expertise in this field positions you to create innovative solutions that leverage the power of natural language understanding. I encourage you to explore further, fine-tune your ChatGPT implementations, and create experiences that truly resonate with your audience. The future of AI-driven conversations is at your fingertips. Happy coding!