How To Use ChatGPT API in PHP Using guzzlehttp Tutorial

Reading Time: 5 minutes

Using GuzzleHTTP to integrate the ChatGPT API into PHP provides a streamlined and fast solution to harness advanced natural language processing capabilities in your applications. We’ll walk you through a step-by-step procedure for using the ChatGPT API in PHP, utilising the GuzzleHTTP framework for seamless connection with the API endpoint.

Before we get into the details, it’s important to grasp the importance of including ChatGPT into your PHP projects.

ChatGPT, created by OpenAI, allows your apps to interpret and generate human-like prose, making it an effective tool for developing intelligent chatbots, language translation services, and more.

Read More: How To Use ChatGPT API in PHP Using cURL Tutorial

Let’s get started.

What is ChatGPT?

ChatGPT is a language model developed by OpenAI. It is part of the GPT (Generative Pre-trained Transformer) series, specifically GPT-3.5-turbo. ChatGPT is designed to generate human-like responses based on the input it receives.

Key features of ChatGPT include:

  • Natural Language Understanding
  • Contextual Understanding
  • Versatility
  • Large-scale Pre-training

Create an Account and Generate OpenAI API Key

To make use of ChatGPT, create an account on OpenAI platform to generate API keys.

You have to pass an email address and password. It will send you a verification email and later on asks for basic information like Name, Phone number, DOB etc.

Once your account will be set successfully

Click on Create New Secret Key

Copy generated secret key.

Composer Package Installation “guzzlehttp”

Open project terminal and run this command,

composer require guzzlehttp/guzzle

It will install package and setup vendor folder into project.

Read More: How To Connect CodeIgniter 4 with Multiple Databases?

Use of ChatGPT API in PHP Using “guzzlehttp”

Here, you can see we have the complete source code of ChatGPT API Integration in PHP.


ini_set("display_errors", 1); // Print errors if any

require "vendor/autoload.php"; // Include the Composer autoloader

use GuzzleHttp\Client;

$openAISecretKey = "sk-UKlics6Ct2So5noVWWGzT3B...";
$apiUrl = "";

$search = "How artificial intelligence is transforming the world?";

// Set headers
$headers = [
    "headers" => [
        "Content-Type" => "application/json",
        "Authorization" => "Bearer " . $openAISecretKey,

// Set data
$data = [
    "json" => [
        "model" => "gpt-3.5-turbo",
        "messages" => [
                "role" => "user",
                "content" => $search,
        "temperature" => 0.5,
        "max_tokens" => 200,
        "top_p" => 1.0,
        "frequency_penalty" => 0.52,
        "presence_penalty" => 0.5,
        "stop" => ["11."],

$client = new Client();

// Merge headers with the data
$options = array_merge($data, $headers);

// Make the API request using Guzzle
$response = $client->post($apiUrl, $options);

// Decode and print the API response
$result = json_decode($response->getBody(), true);

echo "<pre>";


You will get all keys in response.

To get only proper message output of your query,

echo $result['choices'][0]['message']['content'];

That’s it.

We hope this article helped you to learn about How To Use ChatGPT API in PHP Using guzzlehttp Tutorial Example in a very detailed way.

Online Web Tutor invites you to try Skillshike! Learn CakePHP, Laravel, CodeIgniter, Node Js, MySQL, Authentication, RESTful Web Services, etc into a depth level. Master the Coding Skills to Become an Expert in PHP Web Development. So, Search your favourite course and enroll now.

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.

Sanjay KumarHello friends, I am Sanjay Kumar a Web Developer by profession. Additionally I'm also a Blogger, Youtuber by Passion. I founded Online Web Tutor and Skillshike platforms. By using these platforms I am sharing the valuable knowledge of Programming, Tips and Tricks, Programming Standards and more what I have with you all. Read more