2

OK, I've been going nuts with this. I'm outputting a JSON from PHP, and the JSON View extension, for both Chrome and Firefox, is claiming it's invalid. Both extensions work correctly on JSON View's example, so it seems likely that there actually is something wrong with my JSON – but I have no idea what.

The Firefox version has an error message:

There was an error parsing the JSON document. The document may not be well-formed.

The Chrome version lacks such error messages, but still prints the JSON as plaintext.

I am setting the header, like so: header('Content-Type: application/json'); I've checked the response header in Firebug and Chrome's development tools; it is correctly set in both cases. Removing that hides the error message in the Firefox version and the plaintext is not in a monospace font, but that's it.

Complete request headers:

Accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Charset:ISO-8859-1,utf-8;q=0.7,*;q=0.3
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8
Cache-Control:max-age=0
Connection:keep-alive
Cookie:msgPublishID=1347362550,1345649049; logout_rem=1; sh_rand=625703e7f9f9e03efabaef56f52ff97d7f68bc67; username=kryan; password=f85720746a490ece4dd7a945f5c9ed8e25b15f1f; fullname=Kevin+Ryan; user_type=1
Host:localhost
Pragma:no-cache
User-Agent:Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.89 Safari/537.1

Complete response headers:

Connection:Keep-Alive
Content-Length:371
Content-Type:application/json
Date:Thu, 27 Sep 2012 19:12:52 GMT
Keep-Alive:timeout=5, max=99
Server:Apache/2.4.2 (Win32) OpenSSL/1.0.1c PHP/5.4.4
X-Powered-By:PHP/5.4.4

I've gone through many variations on the JSON itself, but I can't imagine that it's the JSON's problem when something as simple as this:

{"session":"expired"}

is still failing. I've checked repeatedly; that is literally the entirety of the server's response, but JSON View still complains. For more complicated JSONs, I've been using

echo json_encode($output, JSON_PRETTY_PRINT);

where $output is an associative array; the output looks completely right but JSON View is still complaining. This is the only echo in the file that isn't commented out.

So what on earth could be going wrong here? I really need JSON View; I work with very large JSONs constantly and the ability to collapse and expand objects and arrays is critical to debugging my application. This online JSON viewer seems to work, but my productivity is going to take a hit if I have to copy and paste the output of these PHP files every time I'm testing them.

EDIT: One thing I have found that works is if I do this:

<?php
    header('Content-Type: application/json');
    die('{"debug":true}');
    // remainder of the program as-is, starting with...
    require('dbinfo.php');

If I then go with this:

<?php
    header('Content-Type: application/json');
    require('dbinfo.php'); // note this comes before the die statement
    die('{"debug":true}');
    // remainder of the program as-is

I get the error again.

So this implies that dbinfo.php is causing the problem.

EDIT: Sorry, I've removed dbinfo.php from this question because it might possibly contain sensitive data that should not be public (even though I stripped out the obvious things). Since the content of dbinfo.php wasn't relevant, it just seems safer to remove it. See my answer below.

KRyan
  • 7,308
  • 2
  • 40
  • 68
  • Could you please provide the additional HTTP headers being sent back and forth for the request and the response? – Lior Cohen Sep 27 '12 at 19:30
  • Do you have any errors in your error log? Have you checked firebug or something to see the headers being sent to the browser? Only thing I can think of is if something inadvertently was already sent so headers can't be modified. – Pitchinnate Sep 27 '12 at 19:34
  • what are you using on the client side to parse the json string? – Flosculus Sep 27 '12 at 19:39
  • @LiorCohen: Sure, added those in. Let me know if anything else is relevant. – KRyan Sep 27 '12 at 19:42
  • @Pitchinnate: As I said in the question originally, yes, I have checked in Firebug that the headers are being sent to the browser and appear to be received. – KRyan Sep 27 '12 at 19:42
  • @Flosculus: Again, as stated in the question originally, I'm using the JSON View extension for Firefox and Chrome. – KRyan Sep 27 '12 at 19:43
  • I have JSON view on chrome and i used this header('Content-Type: application/json'); $array = array( 'session' => 'expired', ); echo json_encode($array,JSON_PRETTY_PRINT); and it works fine. So not sure what the issue would be. – Pitchinnate Sep 27 '12 at 19:52
  • What does your packet sniffer say? – Ignacio Vazquez-Abrams Sep 27 '12 at 19:54
  • @Pitchinnate: Yes, sorry if that was unclear: I doubt it's the content of the JSON that's causing problems. I posted the content to try to demonstrate why I believe this. What I'm hoping to discover is what *else* could cause this. – KRyan Sep 27 '12 at 20:26
  • @IgnacioVazquez-Abrams: Don't have one/have never used one. Can you recommend one/give quick tips on how to use one? I don't really know a lot of details about networks. – KRyan Sep 27 '12 at 20:27
  • 1
    @KRyan: [Wireshark](http://www.wireshark.org/) – Ignacio Vazquez-Abrams Sep 27 '12 at 20:28
  • Have you verified the _content_ of your replies in Firebug/Devtools, as opposed to just checking your PHP code? Since requiring `dbinfo.php` breaks the JSON display, I suspect your script dies before producing the actual JSON output. – lanzz Sep 27 '12 at 20:56
  • @lanzz: Of course I have. For that matter, the output to the screen, even though not pretty printed by JSON View, is all there. – KRyan Sep 27 '12 at 20:57
  • Can you `curl -i ` or `wget -qSO ` and add the full output received to your question? – lanzz Sep 27 '12 at 21:00
  • @lanzz: I figured out the problem, so I don't think I will, but I was wondering: can you do those things from a Windows machine? – KRyan Sep 27 '12 at 21:13
  • 1
    Both have Windows binaries: [curl](http://curl.haxx.se/download.html#Win32), [wget](http://gnuwin32.sourceforge.net/packages/wget.htm). Just in case it's not clear, those are command-line tools, you will be running them in `cmd.exe`. – lanzz Sep 28 '12 at 06:03

1 Answers1

1

Argh.

I figured it out: the BOM was screwing things up, but of course it was also being completely invisible. Checking and changing the encoding to UTF-8 without BOM fixed the problem entirely.

I don't know if this BOM thing is a problem with PHP's design or Unicode's design, but it is certainly obnoxious.

KRyan
  • 7,308
  • 2
  • 40
  • 68