简体   繁体   中英

output a json file response instead of an echo response in php

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM