Last active
February 21, 2023 16:58
-
-
Save kirilkirkov/32d156040667ef2a6fce1841df9b2ce3 to your computer and use it in GitHub Desktop.
Access ChatGPT through PHP Using Curl Api. Easy Use of GPT3 API with Curl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
define("CHATGPT_API_KEY", "your-key-goes-here"); | |
$data = array( | |
"model" => "text-davinci-003", | |
"prompt" => "What is PHP and how it works?", // Your question or request | |
"temperature" => 0.5, | |
"max_tokens" => 500 | |
); | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_URL, 'https://api.openai.com/v1/completions'); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); | |
curl_setopt($ch, CURLOPT_POST, 1); | |
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data)); | |
$headers = array(); | |
$headers[] = 'Content-Type: application/json'; | |
$headers[] = 'Authorization: Bearer ' . CHATGPT_API_KEY; | |
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); | |
$response = curl_exec($ch); | |
if (curl_errno($ch)) { | |
echo 'Error:' . curl_error($ch); | |
} | |
curl_close($ch); | |
echo $response; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Expected json result after php GPT3 request: