0

Can anyone shed any light on why

http://jsonlint.com/

and

http://jsonformatter.curiousconcept.com/

is giving invalid json from URL 1 below but not URL 2? They are both the same JSON generating code. The only difference is one is HTTPs and one is HTTP.

https://www.discussthemarket.com/dev/
JSON.parse: unexpected character at line 1 column 1 of the JSON data

and also

http://www.lambwatch.co.uk/json.htm
Valid JSON

Both have the same JSON generating code behind them, exact same code, but when I put the URLs into

http://jsonlint.com/

for validation, the https site is coming back with a parse error!?

Also, when I do

$json = json_decode(file_get_contents("https://www.discussthemarket.com/dev/"));
$json is  NULL

However

$json = json_decode(file_get_contents("http://www.lambwatch.co.uk/json.htm"));
$json is the object as you'd expect

Can anyone shed any light on this?

Daniel Procter
  • 188
  • 1
  • 2
  • 16
  • 1
    Neither of those functions perform any network requests nor care where the data came from, so no. – Dan Smith Feb 09 '15 at 17:23
  • 1
    Could you share that parse error with us? – castis Feb 09 '15 at 17:23
  • Wait you're putting the URL's in to http://jsonlint.com/? This is a bug with the website then - I pasted the strings from both sites and they validate fine. – Dan Smith Feb 09 '15 at 17:25
  • @Bulk yes, apparently you can put a URL in there for validation. – Daniel Procter Feb 09 '15 at 17:26
  • 1
    @DanielProcter you need to take it up with http://jsonlint.com/ then - we can't help you futher here, there is no code issue. – Dan Smith Feb 09 '15 at 17:27
  • @Bulk, i've added some more info which would suggest it is something more, as trying to json_decode the two URLs results in different behaviour – Daniel Procter Feb 09 '15 at 17:28
  • 1
    Ah yeah this is a whole other thing then - this is a network issue trying to get data from a https site - this isn't related to the JSON at all. – Dan Smith Feb 09 '15 at 17:29
  • @castis i've added the json error to the question. Here it is JSON.parse: unexpected character at line 1 column 1 of the JSON data – Daniel Procter Feb 09 '15 at 17:32
  • @DanielProcter The issue is you're getting NO data from the https site, not invalid data. Try echoing the contents of just the `file_get_contents` with out passing it though `json_decode`. This is 100% not related to JSON in any way. – Dan Smith Feb 09 '15 at 17:34
  • @Bulk - aye, tis weird. Any thoughts at all? I've compared server headers and they are the same except the https is 99 content length while the http is 96 characters long... Where's that extra stuff?! That is surely it – Daniel Procter Feb 09 '15 at 17:35
  • do a `var_dump()` on the results from both sites (with out passing it to any json_decode()) see what you get. – Dan Smith Feb 09 '15 at 17:36
  • @Bulk - I've done a var dump and confirmed there's 3 more in length.. As rjdown has confirmed below. looks like i have a rogue BOM. – Daniel Procter Feb 09 '15 at 18:50

1 Answers1

1

Your problem is that the HTTPS server is adding a UTF8 BOM character to the start of the output, therefore invalidating the expected JSON response. Without seeing the code, it's unclear why, but it's likely a header issue.

If you're unable to solve it server-side, you can always simply remove it at the other end. Here is an example

<?php

$response = file_get_contents('https://www.discussthemarket.com/dev/');
$json = remove_utf8_bom($response);
var_dump(json_decode($json));

function remove_utf8_bom($text) {
    $bom = pack('H*', 'EFBBBF');
    $text = preg_replace("/^$bom/", '', $text);
    return $text;
}
Community
  • 1
  • 1
rjdown
  • 9,162
  • 3
  • 32
  • 45
  • Thanks rjdown You've nailed it. I will do some digging and try and get rid of that BOM server side! Appreciate it! Will post back when i've fixed to confirm answer. Much appreciated. – Daniel Procter Feb 09 '15 at 18:50
  • Fixed. I had [no end of line] issues in the generating code. Sorted. Thank you! – Daniel Procter Feb 09 '15 at 19:01