Looking for a simple procedural php function for sending a message and getting a response. Don’t have CURL or Guzzle or GIT.
You could use PHP’s built in CURL library, but i recommends using non blocking languages with OpenAI as sometimes load times can be a bit much. There are a lot of 3rd party packages the makes development a lot quicker with other languages, although I havent looked at composer in a long time.
Edit: I just saw you can’t use cURL, why is that? I would look into file_get_contents, it’s primarily used for get request but you can also use it for POST request.
Can’t say I’d recommend it, but if you must:
<?php $openaiApiKey = 'sk-....'; $prompt = 'Hello world'; $data = [ 'model' => 'gpt-3.5-turbo', 'messages' => [ ['role' => 'user', 'content' => $prompt], ], ]; $options = [ 'http' => [ 'header' => "Content-Type: application/json\r\nAuthorization: Bearer $openaiApiKey", 'method' => 'POST', 'content' => json_encode($data), ], ]; $context = stream_context_create($options); $response = file_get_contents('https://api.openai.com/v1/chat/completions', false, $context); $response = json_decode($response); $response = $response->choices->message->content; echo $response;
Why wouldn’t you recommend it?
It’ll work, but I’d recommend using a library like https://github.com/openai-php/client for cleaner code, streaming support, etc. But you’ve got some pretty narrow restrictions so not sure that’s feasible
Thanks Phil. I keep my code as tightly under my control as possible. Using third-parties creates exposure. I appreciate the advice.
I would highly recommend considering the option of asking ChatGPT (including free version) the same question in order to obtain the answer you seek. ChatGPT has the potential to provide you with a correct response.
Thanks for that advice. I have send an email to firstname.lastname@example.org but have not heard back from them. I appreciate your attempts to help me.
I didn’t mean their support team, but ChatGPT itself. But… all good.
I hope you solved the problem.