ItSolutionStuff.com

How to Use ChatGPT API in PHP?

By Hardik Savani • May 14, 2024
PHP

Hey Folks,

This post is focused on how to use chatgpt api in php. This article will give you a simple example of how to use openai api in php. This article goes in detailed on php openai api curl request. This tutorial will give you a simple example of php chatgpt api example.

In this example, i will show you step by step how to use chatgpt/openai api in php. we will create new account with OpenAI and then we will run simple curl request to getting data from chatgpt. here, i explained step to make it done this example.

Step 1: Create OpenAI Account

Here, you need to follow few things to create new account with OpenAI.

1. Go to https://openai.com and create new account on it.

2. After login, click on right side corner "Personal" and then click on "View API keys".

3. Then there is a "Create new secret key" button and click on it.

4. Now, add app name and generate new API key and copy it. we will use it on our PHP App.

Step 2: Create index.php

Here, you need to add OpenAI secret key on $openAISecretKey variable. then just create following file with code.

index.php

<?php

$openAISecretKey = "sk-lFIxlfTFsWk3uUlQe4zCT3B...";

$search = "Give me 2 words related to php";

$data = [

"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."],

];

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, 'https://api.openai.com/v1/chat/completions');

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

curl_setopt($ch, CURLOPT_POST, 1);

curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));

$headers = [];

$headers[] = 'Content-Type: application/json';

$headers[] = 'Authorization: Bearer '.$openAISecretKey;

curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$response = curl_exec($ch);

if (curl_errno($ch)) {

echo 'Error:' . curl_error($ch);

}

curl_close($ch);

print_r($response);

Run PHP File:

All the required steps have been done, now you have to type the given below command and hit enter to run the Laravel app:

php -S localhost:8000

Now, Go to your web browser, type the given URL and view the app output:

http://localhost:8000/index.php

Output:

{

"id": "chatcmpl-7gZQtOJv42s039hgaF3a..",

"object": "chat.completion",

"created": 1690380335,

"model": "gpt-3.5-turbo-0613",

"choices": [

{

"index": 0,

"message": {

"role": "assistant",

"content": "

1) Web development

2) MySQL

"

},

"finish_reason": "stop"

}

],

"usage": {

"prompt_tokens": 15,

"completion_tokens": 8,

"total_tokens": 23

}

}

I hope it can help you...

Tags: PHP
Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

📺 Subscribe on YouTube

We Are Recommending You

How to Change PHP Version in Ubuntu Server?

Read Now →

How to Calculate Age from Date of Birth in PHP?

Read Now →

How to Get Full Day Name from Date in PHP?

Read Now →

How to Remove First Element from Array in PHP?

Read Now →

PHP Curl Request with Username and Password Example

Read Now →

PHP Curl Get Request with Parameters Example

Read Now →

PHP Convert XML to JSON Example

Read Now →

How to Convert Array to JSON in PHP?

Read Now →

PHP JQuery Ajax Post Request Example

Read Now →

PHP Dropzone Display Existing Files from Server Example

Read Now →

PHP MySQL Column Sorting Example Tutorial

Read Now →

PHP MongoDB CRUD Operation Example

Read Now →

PHP MySQL Login with Google Account Example

Read Now →

PHP Ajax Multiple Image Upload with Preview Example

Read Now →

PHP Capture Screenshot of Website from URL Example

Read Now →