How to use ChatGPT API with PHP Programing Language

Rumman Ansari   2023-03-19   Developer   web development > ChatGPT API   529 Share

As artificial intelligence (AI) continues to evolve, its applications in various industries have become increasingly popular. One such application is text generation, which is used for various purposes such as writing product descriptions, generating chatbot responses, and creating content for websites. In this blog post, we will explore how to use OpenAI's API to generate text completions using PHP.


<?php

// Generated by curl-to-PHP: http://incarnate.github.io/curl-to-php/
$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, 'https://api.openai.com/v1/completions');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
$postdata = array("model"=> "text-davinci-001",
  "prompt"=> "Seo description for java tutorial 150 words",
  "temperature"=> 0.4,
  "max_tokens"=> 1400,
  "top_p"=> 1,
  "frequency_penalty"=> 0,
  "presence_penalty"=> 0);
$postdata = json_encode($postdata);
curl_setopt($ch, CURLOPT_POSTFIELDS,  $postdata);

$headers = array();
$headers[] = 'Content-Type: application/json';
$headers[] = 'Authorization: Bearer  PROVIDE_YOUR_CHATGPT_KEY_HERE';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$result = curl_exec($ch);
if (curl_errno($ch)) {
    echo 'Error:' . curl_error($ch);
}
curl_close($ch);

$result = json_decode($result , true);
echo '<pre>';
print_r($result);

?>

This is a PHP code that uses the cURL library to send a POST request to the OpenAI API endpoint for generating text completions.

Here's a step-by-step breakdown of what the code does:

  1. Initializes a new cURL session using the curl_init() function and assigns it to the $ch variable.
  2. Sets the API endpoint URL using curl_setopt() with the CURLOPT_URL option.
  3. Sets the CURLOPT_RETURNTRANSFER option to 1, which tells cURL to return the response as a string instead of outputting it directly.
  4. Sets the CURLOPT_POST option to 1 to indicate that this is a POST request.
  5. Defines an associative array called $postdata with various parameters required by the OpenAI API, such as the model to use, the prompt for which text is to be generated, and the maximum number of tokens to be generated.
  6. Encodes the $postdata array as a JSON string using json_encode().
  7. Sets the POST data to be sent with the request using curl_setopt() with the CURLOPT_POSTFIELDS option.
  8. Defines an array called $headers with two elements: Content-Type and Authorization. The Content-Type header specifies that the request data is in JSON format, and the Authorization header contains the OpenAI API key needed to authenticate the request.
  9. Sets the request headers using curl_setopt() with the CURLOPT_HTTPHEADER option.
  10. Sends the POST request to the OpenAI API using curl_exec().
  11. Checks if there was an error during the request using curl_errno() and outputs an error message if there was.
  12. Closes the cURL session using curl_close().
  13. Decodes the response from the OpenAI API, which is in JSON format, into an associative array using json_decode().
  14. Prints the result using print_r() within <pre> tags to format the output in a readable manner.

In conclusion, the above PHP code demonstrates how to use OpenAI's API to generate text completions using PHP. With the help of this code, you can easily generate high-quality text content for various purposes such as writing product descriptions, generating chatbot responses, and creating content for websites. This technology is still in its early stages, and we can expect further advancements in the field of AI text generation in the near future. So, if you want to explore the capabilities of AI text generation, you can start experimenting with OpenAI's API and take advantage of its powerful text generation capabilities.