2015-04-02 37 views
0

下面的代码在php标签中回显出{“status”:200,“status_message”:“在数据库中输入的详细信息”,“data”:121}到提交数据的网页的顶部。一切都好! 但我怎么能把它作为'any.json'文件返回。 我经常看到这些出现在网页的在Windows 7等 底部我曾尝试加入输出一个json文件响应而不是回显响应php

$fp = fopen('results.json', 'w'); 
fwrite($fp, json_encode($response)); 
fclose($fp); 

我也试过FWRITE( “yourjson.json”,json_encode($响应));

但似乎没有任何事情发生(我在注释掉echo $响应时)。如果您有任何建议,我会非常感激,谢谢。

<?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); 
    } 
?> 

上面的代码将数据发送到下面的文件,我也试过把文件写入代码到这个,但再次无济于事。

<?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; 

    } 
?> 

除了响应以外,一切都很好。 最高的php代码从同一个php文件中的HTML表单接收它的值。这只是简单的表单提交4个值,名称,电子邮件,密码,状态。

谢谢。

+0

如果响应是JSON,则不必再次对其进行编码。 – 2015-04-02 14:37:02

+0

您的意思是? http://stackoverflow.com/questions/1465573/forcing-to-download-a-file-using-php?lq=1 – 2015-04-02 14:38:50

+0

我也试过$ fp = fopen('results.json','w'); fwrite($ fp,$ response); fclose($ fp);响应不是在服务器上,而是由几行另一个php文件生成,具体取决于提交的输入。它真的只是回报一切都很好或一切都不好,即404代码等。谢谢你的建议。我继续寻求解决方案。 – 2015-04-02 15:05:55

回答

0

搜索后,我发现这个代码有效。 在标题'Pass_On'

注释掉// echo $ json_response; 并添加......创建一个名为file1.json的文件 将其放置到此文件中的$ json_response变量的内容。

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

然后与其他代码部分,发送数据的代码的顶部块。 在结尾处注释掉echo $ response并添加以下代码。

$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); 

所以数据从从发送到数据库,如果输入正确则返回到投稿网站称为file1.json自动JSON文件。

希望在某个时候帮助某人。感谢其他贡献者的热心帮助和建议。

+0

SORRY:顶部应该是.......................................... ............ $ fp = fopen('file1.json','w +'); fwrite($ fp,$ json_response); fclose($ fp); $ file ='file1.json'; file_put_contents($ file,$ json_response); //获取这个变量$ json_response,正确是很重要的。 header(“Content-type:application/json”); header('Content-Disposition:attachment; filename =''。basename($ file)。''''); header('Content-Length:'。filesize($ file)); readfile($ file); curl_close($ client); – 2015-04-03 10:14:32