DEV Community

Sh Raj
Sh Raj

Posted on • Updated on

Creating AI Chatbot Using ChatGPT API with PHP: A Step-by-Step Guide

To use the ChatGPT API with PHP, you can follow the following steps:

First, you need to create an HTTP client that can send requests to the API endpoint. You can use PHP's built-in curl library for this purpose. Here's an example code snippet:

// Create a new cURL resource
$ch = curl_init();

// Set the API endpoint URL
$url = "https://api.openai.com/v1/engine/davinci-codex/completions";

// Set the request headers
$headers = array(
    "Content-Type: application/json",
    "Authorization: Bearer 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"
);

// Set the request data
$data = array(
    "prompt" => "Hello, how are you?",
    "max_tokens" => 5
);

// Set the cURL options
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

// Send the request and get the response
$response = curl_exec($ch);

// Close the cURL resource
curl_close($ch);

// Print the response
echo $response;
Enter fullscreen mode Exit fullscreen mode

In this code snippet, we are creating a new cURL resource and setting the API endpoint URL, request headers, and request data. We are then setting the cURL options and sending the request using the curl_exec function. Finally, we are printing the response.

You can modify the $data array to include your own prompt and parameters. For example, you can set the temperature parameter to control the creativity of the generated text.

You can parse the response JSON to extract the generated text. For example:

// Parse the response JSON
$response_data = json_decode($response, true);

// Get the generated text
$generated_text = $response_data["choices"][0]["text"];

// Print the generated text
echo $generated_text;
Enter fullscreen mode Exit fullscreen mode

In this code snippet, we are parsing the response JSON using the json_decode function and extracting the generated text from the response data. We are then printing the generated text.

I hope this helps you get started with using the ChatGPT API with PHP!

Top comments (0)