0

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)){
            $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?

3
  • 2
    The header() function sends headers to the browser. It will never, in any way, put anything into a file or string. If you want that header in the file, you need to prepend it to the $dataResults string. Commented Feb 19, 2011 at 3:18
  • 1
    Also, save yourself some hassle by using the json_encode() function. :-) Commented Feb 19, 2011 at 3:19
  • Why are you half writing the JSON by hand and half using json_encode? Why not build the structure you want then encode the whole thing? Commented Feb 19, 2011 at 3:21

3 Answers 3

3

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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).

Comments

-3

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.

1 Comment

No it doesn't. That code produces the same output with or without the header call. I ran it both ways to be sure. There is no relation between your text/xml content-type HTTP header and the <?xml line in the XML file which is just part of XML markup created by the DOM module.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.