The code below, within the php tags, echos out {"status":200,"status_message":"details entered in database","data":121} unto the top of the webpage that submitted the data. All fine! But how can I have it return it as a 'any.json' file. I often see these appear at the bottom of webpages in Windows 7 etc. I have tried adding
$fp = fopen('results.json', 'w');
fwrite($fp, json_encode($response));
fclose($fp);
I also tried fwrite("yourjson.json",json_encode($response));
but nothing appears to happen (I commented out the echo $response when doing this). If you have a suggestion please I would very much appreciate it, thank you.
<?php
if (isset($_POST['submit']))
{
$fields = array(
'name'=>$_POST['name'],
'email'=>$_POST['email'],
'password'=>$_POST['password'],
'status'=>$_POST['status']
);
$postvars='';
$sep='';
foreach($fields as $key=>$value)
{
$postvars.= $sep.urlencode($key).'='.urlencode($value);
$sep='&';
}
$url = "http://www.anywebpage.com/rest/index.php";
$client = curl_init($url);
curl_setopt($client,CURLOPT_POST,count($fields));
curl_setopt($client, CURLOPT_POSTFIELDS, $postvars);
curl_setopt($client, CURLOPT_RETURNTRANSFER,true);
$response = curl_exec($client);
$result = json_decode($response);
echo $response;
curl_close($client);
}
?>
The above code sends the data to the file below, I have also tried putting the file write code into this but again to no avail.
<?php
$page_title = 'Pass_On';
//process client request (VIA URL)
header("Content = Type:application/json");
//include("function.php");
if (!empty($_POST['name']))
{
$name = $_POST['name'];
$email = $_POST['email'];
$password = $_POST['password'];
$status = $_POST['status'];
require_once('mysqli_connect.php');
$sql = "INSERT INTO usersRest(name, email, password, status) VALUES ( '$name', '$email', '$password', '$status')";
$r = @mysqli_query ($dbc, $sql);
//$qur = mysqli_query($dbc, $sql);
$id = mysqli_insert_id($dbc);
deliver_response(200, "details entered in database", $id);
}
function deliver_response($status,$status_message,$data)
{
header ("HTTP/1.1 $status $status_message");
$response['status']=$status;
$response['status_message']=$status_message;
$response['data']= $data;
$json_response=json_encode($response);
echo $json_response;
}
?>
All is well except for the response. The top php code receives it's values from an HTML form in the same php file. It's just straight forward Form submitting the 4 values, name, email, password,status.
Thank you.