ChatGPT Simple PHP OpenAI Example
To use OpenAI in PHP, you will need to use an API provided by OpenAI. You can sign up for access to the API on the OpenAI website here. Keep in mind there are many ways to use ChatGPT with PHP and I am only presenting only one here.
Once you have access to the API, you can use the PHP CURL
function to send requests to the API and receive responses. Your host MUST have CURL installed along with the “fopen” active and ability to use “file_get_contents”. If your host does not have this or you are looking for a great host, check out Web Hosting Recon.
At the bottom of this tutorial, you will find a ready-made package to download. Simply add your own OpenAI API key.
Here is an example of how you might use the API to generate ChatGPT response text using text-davinci-003:
<?php
//
// OpenAI ( https://platform.openai.com/signup )
// API Key ( login at above url )
// Get more tips @ terryjett.com
//
$file = fopen('content/data.txt', 'w');
$TEXT = 'what is html5 and give example. in example code, add two headings and two paragraphs.';
$API_KEY = 'sk-YOUR-KEY-HERE';
$header = array(
'Authorization: Bearer '.$API_KEY,
'Content-type: application/json',
);
$params = json_encode(array(
'prompt' => $TEXT,
'model' => 'text-davinci-003',
'temperature' => 0.5,
'max_tokens' => 400,
'top_p' => 1.0,
'frequency_penalty' => 0.8,
'presence_penalty' => 0.0
));
$curl = curl_init('https://api.openai.com/v1/completions');
$options = array(
CURLOPT_POST => true,
CURLOPT_HTTPHEADER =>$header,
CURLOPT_POSTFIELDS => $params,
CURLOPT_RETURNTRANSFER => true,
);
curl_setopt_array($curl, $options);
$response = curl_exec($curl);
$httpcode = curl_getinfo($curl, CURLINFO_RESPONSE_CODE);
if(200 == $httpcode){
$json_array = json_decode($response, true);
$choices = $json_array['choices'];
foreach($choices as $v){
fwrite($file, $v['text'] . "\n");
}
}
?><!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>ChatGPT API Example - TerryJett.com</title>
<style>
textarea#styled {
width: 800px;
height: 200px;
border: 3px solid #b399ff;
padding: 5px;
font-family: Tahoma, sans-serif;
background-image: url(images/bg.png);
background-size:cover;
background-repeat: no-repeat;
}
</style>
</head>
<body>
<?php $datafile = file_get_contents("content/data.txt");
echo $datafile;
?>
<p>Here is the above content placed into a textarea:</p>
<textarea id="styled">
<?php $datafile = file_get_contents("content/data.txt");
echo $datafile;
?>
</textarea>
<p>Visit <a href="https://terryjett.com" target="_blank">terryjett.com</a> for more.
</body>
</html>
In this example, we first set some variables for our API key, the text we want to generate more text from, the length of the generated text, and the “text-davinci-003” model. You could also use other models like these.
We then create an array of data to send to the API, including our prompt, length, and model.
Next, we set some headers for our API request, including the content type and authorization token.
We then use curl
to send the request to the API, passing in our data and headers, and storing the response in a variable.
Finally, we decode the response JSON and extract the generated text from the choices
array.
When you run the PHP, you will get an output like this:
This is just a basic example, and there are many other ways you can use GPT-3 in your PHP projects. Be sure to check the OpenAI API documentation for more information and examples.
COMING SOON: Similar tutorial for “gpt-3.5-turbo” which is lower cost to use. Be sure to subscribe to our newsletter to get updates when we release new tutorials and resources.
Complete PHP example to use ChatGPT and OpenAI.
Disclaimer:
Before putting any type of code or advice into production from this site, you should consult a professional. The code and advice here is for educational purposes only. It is your responsibility to ensure code safety.