Dillinger
Dillinger

Reputation: 1903

json_decode can't deserialize properly

I've made a RestAPI that working pretty well, at the top of my script (the core file), I have declared this:

header('Content-Type: application/json;charset=utf-8');

Now the problem is that when I pass a json from the client to my API. This problem is related on the POST and PUT method, usually. This happean only if I pass this type of json with curl:

curl -i -d '{"test": "èè"}'

How you can see I've some accented letters. So when I call this function:

$params = json_decode(file_get_contents("php://input"), true);

the $params variable return NULL. Instead if my json looks like this:

curl -i -d '{"test": "This is a test"}'

all working good, and the $params variable have this valorization:

array(1) {
  ["test"]=>
  string(14) "This is a test"
}

I guess that the special character cause problem on this function.. Someone have a solution for this?

Upvotes: 0

Views: 134

Answers (1)

Xorifelse
Xorifelse

Reputation: 7911

You could use regex to determine if the input line contains accented characters.

<?php

  preg_match("/[À-ÿ].*/", $input_line, $output_array);

  if(sizeof($output_array) > 0){
    echo "$input_line has accented characters";
  }

?>

You can also take a look at URL encoding, because I think the issue is there.

Upvotes: 2

Related Questions