0

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.

  • If the response is JSON you shouldn't have to encode it again. – Jay Blanchard Apr 02 '15 at 14:37
  • You mean this? http://stackoverflow.com/questions/1465573/forcing-to-download-a-file-using-php?lq=1 – Sergiu Paraschiv Apr 02 '15 at 14:38
  • I did also tried $fp = fopen('results.json', 'w'); fwrite($fp, $response); fclose($fp); The response is not a on a server but is generated by a few lines another php file depending on the input submitted. It really just gives back that all is well or all is not well, ie a 404 code etc. Thank you for your suggests. I continue to seek a solution. – Victor Victoria Apr 02 '15 at 15:05

1 Answers1

0

After searching round I found this code that works. Use this at end of the file with title 'Pass_On'

Comment out the //echo $json_response; And add...... to make a file called file1.json The content of $json_response variable it placed into this file.

$fp = fopen('file1.json', 'w+');
fwrite($fp, $json_response);
fclose($fp);

Then with the other code section, the top block of code that sends the data. Comment out echo $response near the end and add the following code.

$file = 'file1.json';

file_put_contents($file, $response); //getting this variable, $response, correct was important.

header("Content-type: application/json");

header('Content-Disposition: attachment; filename="'.basename($file).'"'); 

header('Content-Length: ' . filesize($file));

readfile($file); 

curl_close($client);

So the data is sent from the from to the database and if it is entered correctly an automatic json file called file1.json is returned to the posting site.

Hope that helps somebody sometime. Thank to the other contributor for their kind help and suggestion.

  • SORRY: The top section should be...................................................... $fp = fopen('file1.json', 'w+'); fwrite($fp, $json_response); fclose($fp); $file = 'file1.json'; file_put_contents($file, $json_response); //getting this variable, $json_response, correct was important. header("Content-type: application/json"); header('Content-Disposition: attachment; filename="'.basename($file).'"'); header('Content-Length: ' . filesize($file)); readfile($file); curl_close($client); – Victor Victoria Apr 03 '15 at 10:14