I am trying to send JSON to an API url. I am sending the json from test.php and I am receiving the data from api.php. I want to log the data into a text file, but nothing is logged, I assume that $data is returning null? In my test.php file it keeps returning "Sent BAD, request terminated." What am I doing wrong? How would I fix this?
test.php
<?php
$ch = curl_init();
$array = array('foo' => 'bar');
curl_setopt($ch, CURLOPT_URL,"https://dropchat.net/api.php");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($array));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$server_output = curl_exec($ch);
curl_close ($ch);
if ($server_output == "OK") {
echo "Sent OK";
} else {
echo "Sent BAD, request terminated.";
}
?>
api.php
<?php
$data = json_decode(file_get_contents('php://input'), true);
$fp = fopen("myText.txt","wb");
fwrite($fp, $data);
fclose($fp);
?>
I assume...don't assume, test. And configure PHP to log errors and warnings to a file.if ($server_output == "OK") {will never betruebecause api.php doesn't echo "OK" (or anything else, for that matter). So that doesn't prove anything one way or another.test.phpbeapi.php?fwrite()should be a string.$datais an array.