Error when using OpenAI API in PHP project

Hi,

I am having an issue with my project. I am using OpenAI’s API to generate text, but I keep getting the following error: “you must provide a model parameter”. I have checked my code and it appears to be correct, but the error persists. I have tried using different models, but the error remains. I have also checked my server configuration and everything seems to be set up properly. Does anyone have any suggestions on how to fix this issue?
html:

<html>
<body>

<form action="generar-planificacion.php" method="post">
  Objetivo de aprendizaje:<br>
  <input type="text" name="objetivo-aprendizaje" required><br>
  Edad de los estudiantes:<br>
  <input type="number" name="edad-estudiantes" required><br>
  Número de clases:<br>
  <input type="number" name="numero-clases" required><br><br>
  <input type="submit" value="Generar planificación">
</form>

</body>
</html>

php:

<?php

// Tu clave de API de OpenAI
$api_key = "xxxxxx";

// Recupera los valores del formulario
$objetivo_aprendizaje = $_POST["objetivo-aprendizaje"];
$edad_estudiantes = $_POST["edad-estudiantes"];
$numero_clases = $_POST["numero-clases"];

// Los parámetros de la solicitud
$params = array(
    "model" => "text-davinci-003",
    "prompt" => "Planificación de clases para " . $objetivo_aprendizaje . " para estudiantes de " . $edad_estudiantes . " años:\n",
    "max_tokens" => $numero_clases * 5,
    "temperature" => 0.5,
);

// Inicia la solicitud cURL
$ch = curl_init("https://api.openai.com/v1/completions");

// Establece la clave de API en la cabecera de la solicitud
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Authorization: Bearer $api_key"));

// Establece los parámetros de la solicitud como el cuerpo de la solicitud POST
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params));

// Establece la opción CURLOPT_RETURNTRANSFER para que la respuesta se devuelva como una cadena en lugar de ser mostrada directamente
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

// Ejecuta la solicitud cURL
$response = curl_exec($ch);

// Cierra la conexión cURL
curl_close($ch);

// Muestra la respuesta
echo $response;

?>

Thanks in advance for any help.

Dont use http_build_query

You need JSON.stringify

How should it be in the code?

// Establece los parámetros de la solicitud como el cuerpo de la solicitud POST
curl_setopt($ch, CURLOPT_POSTFIELDS, JSON.stringify($params));

@jaespinozasj
Sorry - I got the wrong function for vanilla PHP. This will work better

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

Here is an example of some code that works


$api_key = ‘my-secret-API-key’;
$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, ‘https://api.openai.com/v1/completions’);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
“Content-type: application/json”,
“Authorization: Bearer $api_key”
));

$api_params = array(‘model’ => ‘text-davinci-003’,‘prompt’ => ‘Say this is a test’);
$api_query = json_encode($api_params);
curl_setopt($ch, CURLOPT_POSTFIELDS, $api_query);

$api_response = curl_exec($ch);
curl_close($ch);

print_r($api_response);

You can use this PHP client: GitHub - openai-php/client: ⚡️ OpenAI PHP is a supercharged PHP API client that allows you to interact with OpenAI API.