WmZilla - Webmaster and Marketplace

The Next Generation Webmaster and Trade Forum

Integrating the Open AI API in PHP

YaBoiSmokey

New member

0

0%

Status

Offline

Posts

55

Likes

0

Rep

0

Bits

285

3

Months of Service

0%
Hello everyone,

Before we wrap up this 2023, I want to share a PHP function with you for querying the Open AI API.

It's just a query, you should adapt it according to your needs. It's commented to make it easier to understand.

PHP:

function api_query($prompt){

$request_data = array(
"model" => "text-davinci-003",
"prompt" => $prompt,
"max_tokens" => 1000
);

// Convert the request data content into a JSON string
$request_data_json = json_encode($request_data);

// Initialize the cURL session
$ch = curl_init("https://api.openai.com/v1/completions");

// Set cURL request options
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $request_data_json);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"Content-Type: application/json",
"Content-Length: " . strlen($request_data_json),
"Authorization: Bearer " . API_KEY_GPT
));

// Execute the cURL request
$response = curl_exec($ch);

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

// Process the API response
return json_decode($response, true);
}

Here's to a 2024 filled with blessings, new projects, and lots of code..!!! ????????
 

249

6,622

6,642

Top