开发者

PHP - creating JSON but header doesn't appear

I'm creating a JSON string from the results of a mySQL query in PHP. But for some reason the PHP "header" function isn't appending anything when I save the results to a file for sanity checks. Below is the code:

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

    if(mysql_num_rows($result)){
          开发者_StackOverflow中文版  $dataResults = '{"Data":[';
            $first = true;
            $row = mysql_fetch_assoc($result);

            while($row = mysql_fetch_array($result, MYSQL_ASSOC)){
                    if($first) {
                            $first = false;
                    } else {
                            $dataResults = $dataResults . ',';
                    }
                    $dataResults = $dataResults . json_encode($row);
            }
            $dataResults = $dataResults . ']}';
    } else {
            $dataResults = '[]';
    }

    file_put_contents('/Applications/MAMP/htdocs/PHP/results.json', $dataResults);

The output looks o.k., except it is missing the "Content-Type: application/json". What am I doing wrong?


header appends HTTP headers to the web server's HTTP response. It doesn't produce any output or write anything to any file. Files do not have HTTP headers, they are part of the HTTP protocol, the language used to communicate between web servers and browsers.


Why would header() write anything to the file?

header() sets response header information using the response hook in mod_php or whatever the CGI equivalent is if using CGI.

Text files do not contain any meta information other than their encoding (if that).


Since several people have stated that the header function won't write anything to a file I'll show you some code where it does.

         $dom = new DOMDocument("1.0");
     $node = $dom->createElement("markers");
     $parnode = $dom->appendChild($node);

     header("Content-type: text/xml");


    //Iterate through the rows, adding XML nodes for each
     while ($row = @mysql_fetch_assoc($result)){
         $node = $dom->createElement("marker");
         $newnode = $parnode->appendChild($node);
         $newnode->setAttribute("name", $row['bName']);
         $newnode->setAttribute("address", $row['Street'].", ".$row['City']);
         $newnode->setAttribute("lat", $row['lat']);
         $newnode->setAttribute("lng", $row['lng']);
         $newnode->setAttribute("distance", $row['distance']);
         $newnode->setAttribute("Webpage", $row['Webpage']);
     }

     $dom->formatOutput = true;
     $dom->save("/Applications/MAMP/htdocs/PHP/DOM.xml");

The output from that code results in a file like this:

<?xml version="1.0"?>
<markers>
<marker name="Lake House Diagnostic Program " address="123 S. Lake St., Aurora" lat="41.757786" lng="-88.321419" distance="0.000166859373254629" Webpage="http://www.auntmarthas.org/"/>
</markers>

So you see it will produce the header when I create XML output.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜