1

here is my origin code:

$returnArray=array(
    'loginUrl'=>$url."/?page_id=2732",
    'clientNumber'=>$clientNumber,
    'clientName'=>$_SESSION['userName']
);
echo json_encode($returnArray);

and here is my receiving end code:

$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 

$result = trim(curl_exec($ch));

curl_close($ch);

$result = json_decode($result,true);

And finally here is the $result var_dump

string(194) "
{"loginUrl":"http:\/\/www.xxxxxxxxxxxx.co.il\/chtsystem\/?page_id=2732","clientNumber":"11111","clientName":"\u05d0\u05d1\u05d9 \u05d4\u05de\u05d3\u05d1\u05d9\u05e8 - www.yyy-yyyyyyy.co.il"}"

$result comes out null from the decode, when printing json errors I can see "JSON_ERROR_SYNTAX" is on

what is wrong here?

3
  • 2
    You have a new line or a BOM character before output. check that. Commented Nov 16, 2012 at 9:19
  • @MihaiIorga does a leading newline trip json_decode? I don't think it should. Commented Nov 16, 2012 at 9:22
  • @JanDvorak i was to lazy to correct myself, I wanted to say that there is a new line that is generated by an UTF8 BOM character. Commented Nov 16, 2012 at 9:36

1 Answer 1

1

There's nothing wrong in having a leading new line in your JSON string. But you probably have a UTF-8 BOM and json_decode() can't cope with that.

The most likely source for the BOM is your IDE or text editor. Verify its settings and make sure that UTF-8 files are saved without BOM. Such BOM is not mandatory and it normally just creates problems like this one.

Update:

You can replace this:

var_dump($result);

... with this:

var_dump(bin2hex($result));

... to obtain a more accurate view of your JSON string contents. A UTF-8 BOM would reveal itself as:

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

7 Comments

well i have saved both ends using UTF-8 no BOM and still it comes out NULL
Use bin2hex() to determine the exact character before the actual JSON output.
can you elaborate more on how to use ?
@liorr - Alright, I see I'm not being clear. I've updated my answer.
thanks , this is the result string(120) "efbbbf0a3c64697620636c6173733d27657272273ed79ed7a1d7a4d7a820d794d79ed7a9d7aad79ed7a920d7a9d792d795d799203c2f6469763e2d31"
|

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.