[SOLVED] Maybe need a new host: error - We could not parse the JSON body of your request

Hello,
This is just a basic php setup with the API - I keep this error below:

‘We could not parse the JSON body of your request.’

Not quite suRe where it’s going wrong, so thinking it might be my host. if that’s the case, does anybody have any suggestions for a new host wheRe this should work.

[code] <?php
require_once ‘/home/fioo/php/Services/JSON.php’;
$json = new Services_JSON();

// Replace YOUR_API_KEY with your actual API key
$api_key = “MY_Api_key_is_here”;

// Check if the form has been submitted
if (isset($_POST[“input_text”])) {
// Get the input text from the form
$input_text = $_POST[“input_text”];

// Use cURL to send a request to OpenAI’s API
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, “https://api.openai.com/v1/completions”);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode(‘{
“model”: “text-davinci-002”,
“prompt”: “Rewrite this to be more readable for people with dyslexia: ’ . $input_text . '”,
“temperature”: 0.5,
“max_tokens”: 1024,
}’));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_USERPWD, “openai:” . $api_key);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
“Content-Type: application/json”
));
$response = curl_exec($ch);
if (curl_errno($ch)) {
echo ‘Error:’ . curl_error($ch);
}
curl_close($ch);

// Decode the response from JSON to a PHP object
$response_obj = json_decode($response);

echo '<pre>';

print_r($response);
echo ‘’;

// Get the rewritten text from the response
$rewritten_text = $response_obj->choices[0]->text;
}
?>

Rewrite Text for Dyslexia

Rewrite Text for Dyslexia

Enter some text:

<?php if (isset($rewritten_text)): ?>

Results

Original text: <?= $input_text ?>

Rewritten text: <?= $rewritten_text ?>

Changes:

  • Shortened sentences
  • Used simpler words
  • Used active voice
  • Used concrete, specific language
  • Used bullet points or numbered lists
<?php endif; ?> [/code]

Welcome to the community!

The error indicates you’re not sending a valid json. I think it’s because you’re not feeding json_encode a valid associative array…

Here’s an example with PHP and cURL that might help…

Hope this helps!

1 Like

Thank you soo much. Solved.

1 Like