Site icon Antonio Lamorgese

How to invoke OpenAI and ChatGPT with PHP

How to invoke OpenAI and ChatGPT with PHP

How to integrateOpenAI and ChatGPT artificial intelligence with PHP to improve applications and automate processes.
di Antonio Lamorgese

In this article, we’ll explore how to invoke OpenAI and ChatGPT with PHP, the world’s most popular server-side programming language. OpenAI is an artificial intelligence platform that provides a wide range of advanced technologies and models, including the “Natural Language Processing”, of text generation and computer vision. With the help of PHP, you can use these powerful capabilities to enhance your application and automate most of the processes you use for your daily purposes. I’ll show you how to configure the environment, invoke OpenAI models and manage the responses obtained. This way, you will be able to make the most of the power of OpenAI in your PHP project or even in WordPress.

To invoke OpenAI and ChatGPT with PHP, you need to get the API Key on the OpenAI portal. This operation is really simple. In the next paragraph I will explain how to do it.

How to create your own API Key to access OpenAI and ChatGPT

To access the OpenAI APIs and invoke the access methods to the artificial intelligence models offered by the portal, you need to create your own personal API Key. To do it is very simple. First of all access the Secret Key creation page, as it is called by OpenAI, by clicking on this link. Log in, even with your Google credentials if you have them, and click on the “Create new Secret Key” button. At this point, copy and paste the generated code into your personal archive for later use when required.

OpenAI Create New Secret Key
OpenAI Create New Secret Key

In any case, know that when you need it, you can retrieve your Secret Key by accessing the portal again, or you can revoke the previously generated Secret Key at any time by recreating a new one.

Invoking OpenAI with PHP

Once you have obtained your OpenAI API Key you can invoke all its models, and use the functions offered by the library, to invoke OpenAI services and take advantage of artificial intelligence to interact, in a conversational way, with ChatGPT. For example, to invoke a template for text completion, you could use the following code, to ask ChatGPT “What is OpenAI?”, without adding PHP features or libraries:

   $apiKey = "Your OpenAI API Key here…";
   $data = array("model" => "text-davinci-002", "prompt" => "What is OpenAI?");
   $data_string = json_encode($data);
   $ch = curl_init('');
         curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
         curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
         curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
         curl_setopt($ch, CURLOPT_HTTPHEADER, array(
            'Content-Type: application/json',
            'Content-Length: ' . strlen($data_string),
            'Authorization: Bearer ' . $apiKey
   $result = curl_exec($ch);
   $result = json_decode($result, true);
   $generated_text = $result['choices'][0]['text'];
   echo $generated_text;

This way, ChatGPT ‘s text completion algorithm will complete the text based on the specified pattern. This is just one example, but there are many other features available to take advantage of other ChatGPT conversational models as well. In this regard, you can consult the official documentation for more information. In addition to the normal use of ChatGPT with PHP it is possible to invoke the artificial intelligence offered by OpenAI also within your WordPress blog. Now, in the next paragraph, I will explain how to do it.

Invoking OpenAI with WordPress

With WordPress you have two possibilities to invoke the OpenAI API and put ChatGPT into action. The first possibility takes advantage of one of the many plugins available to make API calls to web services. The second possibility takes advantage of a function that resides directly in the WordPress CORE and that plugins also take advantage of considerably. This function is: wp_remote_post(). Now let’s see in summary these two possibilities for invoking the OpenAI API with WordPress.

Read more: “How to create a plugin WordPress

Invoking OpenAI with a Plugin WordPress

Thanks to the power offered by WordPress plugins, you can take advantage of ChatGPT ‘s artificial intelligence to create quality content by taking advantage of its powerful algorithms. One of these plugins is undoubtedly “AIPress ”.

Plugin "AIPress" Description Page
Plugin “AIPress” Description Page

After installing and activating the plugin you can immediately take advantage of the potential offered by the plugin to generate unique and quality content in a very short time. in any case, by following this short video tutorial, you will be able to appreciate the extraordinary features of “AIPress” with WordPress.

AIPress - Introducing the ChatGBT React Plugin for WordPress - Automate Your Content Creation
Plugin “AIPress”

In summary, once the plugin is configured, you can use the OpenAI and ChatGPT APIs directly from within the Gutenberg editor of your WordPress site.

Invoking OpenAI from PHP code in WordPress

To invoke OpenAI from PHP code in WordPress, you need to use the OpenAI API, which provides an endpoint to access all features of the ChatGPT conversational model. Here is an example of code that will ask OpenAI and ChatGPT, “What is ChatGPT?”, with PHP in WordPress:

   $url = '';
   $api_key = 'Your OpenAI API Key here…';
   $args = array(
         'method' => 'POST',
         'headers' => array(
         'Authorization' => 'Bearer ' . $api_key,
         'Content-Type' => 'application/json',
         'body' => json_encode(array(
         'prompt' => 'What is ChatGPT?',
         'max_tokens' => 100,
         'temperature' => 0.5,
   $response = wp_remote_post( $url, $args );
   if ( is_wp_error( $response ) ) {
      // error
      $error_message = $response->get_error_message();
      echo "Something went wrong: $error_message";
   } else {
      // Response
      $data = json_decode( wp_remote_retrieve_body( $response ), true );
      print_r( $data );

Note that this example requires you to enter your OpenAI API Secret Key in the request header. Also, the parameters for the API request can be changed depending on your needs.

Read more: “How to increase website traffic with an plugin WordPress


Invoking OpenAI and ChatGPT with PHP is a simple and powerful way to integrate AI technology into your WordPress application or blog to auto-generate great content. By combining the wide range of models and technologies offered by OpenAI with the flexibility and popularity of PHP, you can leverage the power of OpenAI to automate processes, improve user experience, and deliver new functionality to your apps. I hope this article has given you the knowledge you need to integrate OpenAI into your PHP project and get the most out of this technology and artificial intelligence.

Exit mobile version