Problem with POST variable while connecting to an api

<?php

$ch = curl_init();
$client_id="clientID";
$client_secret="clientSecret";
$language="python3";
$versionIndex="0";

$script=(string)$_POST["script"];

$data="{
    \"clientId\": \"$client_id\",
    \"clientSecret\":\"$client_secret\",
    \"script\":\"$script\",
    \"language\":\"$language\",
    \"versionIndex\":\"0\"
    }";

echo $data;

curl_setopt($ch, CURLOPT_URL, "https://api.jdoodle.com/v1/execute");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,$data);
curl_setopt($ch, CURLOPT_POST, 1);

$headers = array();
$headers[] = "Content-Type: application/json; charset=UTF-8";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$result = curl_exec($ch);
//echo $result;
$data = json_decode($result, TRUE);
print_r($data);

if (curl_errno($ch)) {
    echo 'Error:' . curl_error($ch);
}
curl_close ($ch);

?>

I tried to send the code as string to jdoodle api. I am getting invalid request as output. I think the problem is with $_POST variable. I am getting output when I am not using $_POST variable. so please solve this problem . Is there any other way to send data to an api. Thanks in advance for helping me

And your $script is escaped properly to be used within JSON? Validate JSON first.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.