Reputation: 907
Hi everybody this is the string ( sorry if long ) in php
$JsonSpecificheTitoli='{"titoli":{"TitoloEbay-1-Virtuemart":" Maglia Shirt Adidas Milan tg S M L Per sport d è numerico maniche corte Home ","TitoloEbay-2-EbayIT":" Maglia Shirt Adidas Milan tg M L Ibrahimovic Per sport d è numerico Home ","TitoloEbay-5-EbayIT":" Maglia Shirt Adidas Milan tg XL Per sport d è numerico maniche corte Home ","TitoloEbay-3-EbayDE":" Trikot Shirt Adidas Milan tg S M XL F�r Sport ist numerisch S/S Saison 09 10 ","TitoloEbay-6-EbayDE":" Trikot Shirt Adidas Milan tg L F�r Sport ist numerisch S/S Saison 09 10 Home ","TitoloEbay-4-EbayUK":" Jersey Shirt Adidas Milan tg L XL XXL For sports is numerically Kurze armel "},"specifiche":{"Virtuemart":{"":"Arrivo in 24/48 h con SDA oppure Possibilita di necessita di 4/5 giorni per la spedizione","Maniche":" maniche corte "},"EbayIT":{"":"Arrivo in 24/48 h con SDA oppure Possibilita di necessita di 4/5 giorni per la spedizione","Maniche":" maniche corte "},"EbayDE":{"":"Ankunft in 24/48 h M�glichkeit der SDA oder erfordert 4 / 5 Tage Versandkosten","Arme":" S/S "},"EbayUK":{"":"Arrival in 24/48 h Possibility of SDA or requires 4 / 5 days for shipping","Sleeves":" Kurze armel "}}}';
if I make a json_decode from this string directly with php no problem:
$pr = json_decode($JsonSpecificheTitoli); //-->OK
but If I take it from REQUEST ( from an Ajax request ) it gives me null
$Tt = $_REQUEST['Titoli'];
$TtJ = json_decode ($Tt); // --> it says null
$v=json_last_error(); //--> gives me 5 , is Syntax error?
Strange anyway how the same string in a REquest and copy and printed can turn it bad?
Thanks
Upvotes: 1
Views: 243
Reputation: 197658
$v=json_last_error();
gives me 5 , is Syntax error?
You can check that against the error constants like JSON_ERROR_SYNTAX
, just echo out the value:
echo JSON_ERROR_SYNTAX; # 4
echo JSON_ERROR_UTF8; # 5
So in your case it's an UTF8 error. json_decode
Docs takes an UTF-8 encoded string as input. Just ensure it's UTF-8 encoded and you're fine.
The encoding of $_REQUEST
(and similar input variables) depends on the encoding of the HTTP request (input) that is send into your application which is dependent on the encoding/charset of the original website normally.
There is no way to reliably detect it, so it's better you know it. However the mb_http_input
Docs function can be helpful if you need to detect it.
Obtain the information which encoding is used in the HTTP request to your PHP script and then re-encode the incomming data as necessary before passing them into json_decode
. You can re-encode a string in PHP with the help of the iconv
functionDocs or the Multibyte String functionsDocs.
Additionally you can also verify if the input is valid an valid UTF-8 string, and if not, deny to accept the invalid request to your application. To check if a string is UTF-8, you can run a regular expression on a string:
$isUTF8 = preg_match('//u', $string);
(see also How to detect malformed utf-8 string in PHP?)
Upvotes: 2
Reputation: 198314
Check your encoding. 5 = JSON_ERROR_UTF8.
Also, how do you know they are "same string"? Or are you just assuming? Did you print out serverside what the server is receiving, or do you just know what you're sending and expect it to be the same thing that is received?
Upvotes: 1