Problem using json_decode () and json_encode ()

1

I have these string examples I get as urlencoded POST :

data%5Bst_cartaodetalhes_recb%5D=TID%3A+000000%0ACart%E3o%3A+0000%2A%2A%2A%2A%2A%2A%2A%2A0000%0AAutoriza%E7%E3o%3A+0%0ABandeira%3A+

Another example:

data%5Bst_descricao_cb%5D=Conta+Corrente-+Ita%FA

I want to create an object with this string, but I am not able to work with the encoding, whenever I try to give a json_encode or json_decode it returns me a json_last_error() = 4 JSON_ERROR_SYNTAX and the string is empty or a string only appears with []

The biggest problem is that I can not play the json I get because it's from a third party that sends a hook to process ...

Previously I just saved a log of what was coming in the hook this way:

$raw_data = file_get_contents("php://input");
$post = $_POST;

log_hook( "webhook post".PHP_EOL.json_encode($_POST));
log_hook( "webhook raw".PHP_EOL.$raw_data);

Considerations:

log_hook( "webhook raw".PHP_EOL.$raw_data); Always prints what comes in the request, and special characters are printed as the hexadecimal representation: %C1 = Á

log_hook( "webhook post".PHP_EOL.json_encode($_POST)); It prints the json correctly if the request has no special character of type %C1 = Á

With this I could see that json_encode just did not return a valid json when the hook had any of these "strange" characters.

How do I process these hooks? I tried to use variations of urldecode , rawurldecode , utf8_decode [1], utf8_encode [1] and nothing worked .. the maximum was to convert the character to and still not process json.

[1] Even though the last 2 have nothing to do with the type of encoding.

    
asked by anonymous 02.05.2017 / 18:45

1 answer

2

That way you fix the charset and transform it into json.

$urldec = "data%5Bst_cartaodetalhes_recb%5D=TID%3A+000000%0ACart%E3o%3A+0000%2A%2A%2A%2A%2A%2A%2A%2A0000%0AAutoriza%E7%E3o%3A+0%0ABandeira%3A+";

mb_parse_str(utf8_encode(urldecode($urldec)), $result);
$json = json_encode($result);

echo $json;
    
02.05.2017 / 21:45