Re: [PHP] Malformed UTF-8 Data in JSON [SOLVED]

2010-07-14 Thread Benjamin Hawkes-Lewis
On 15 Jul 2010, at 04:12, Dave M G wrote:
> Yes, stripslashes() was the problem. I've removed it and the code works.
> 
> However, it seems that when I send JSON data from a Javascript file, 
> stripslashes() is necessary. That's why I had it there. I'm not entirely sure 
> what's going on there, so obviously more experimentation is needed.

Presumably, thanks to your PHP settings, you need stripslashes() on all $_GET, 
$_POST, and $_COOKIE input.

http://www.php.net/manual/en/info.configuration.php#ini.magic-quotes-gpc

This does not apply to input from other sources.

--
Benjamin Hawkes-Lewis
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Malformed UTF-8 Data in JSON [SOLVED]

2010-07-14 Thread Dave M G

Jim,

Thank you for responding.

Yes, stripslashes() was the problem. I've removed it and the code works.

However, it seems that when I send JSON data from a Javascript file, 
stripslashes() is necessary. That's why I had it there. I'm not entirely 
sure what's going on there, so obviously more experimentation is needed.


In any case, your suggestion has got me on the next step, so thanks for 
that tip. I'll add 2 cents to your tab!


--
Dave M G

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Malformed UTF-8 Data in JSON

2010-07-14 Thread Jim Lucas
Dave M G wrote:
> PHP Users,
> 
> I'm decoding some JSON data in PHP to convert it into an array.
> 
> However, it's not working, and json_last_error() is returning a value of
> "4", which I believe means "Malformed UTF-8 characters, possibly
> incorrectly encoded".
> 
> I try at every turn in every setting to ensure that all my code and
> connections are in UTF-8.
> 
> But how can I be sure it's valid UTF-8? I've tried using the PHP command
> utf8_encode() on the string, but that hasn't changed anything.
> 
> And can I trust the error message?
> 
> Any help or advice would be much appreciated.
> 
> By the way, here is the code I'm currently testing with. I'm just
> encoding and then decoding a string right in the PHP just to get it to
> work before I even try getting the data from anywhere else.
> 
> $myData ='{"display_name":"Test
> Guy","email":"test...@testaddress.com","timeout":"1279145273"}';
> $myArray1 = json_encode($myData);
> $myArray2 = utf8_encode (stripslashes($myArray1));
> $myArray = json_decode($myArray2, true);
> $jsonerror = json_last_error();
> 

Were you meaning to strip the slashes??  That will break things...  They are
their for a reason.  :)

Try it without that stripslashes() and see what happens.

Also, if on that error page they are using a bitwise numbering scheme it would
not be the last one, it would be the third error message

Value   Constant
1   JSON_ERROR_NONE
2   JSON_ERROR_DEPTH
4   JSON_ERROR_CTRL_CHAR
8   JSON_ERROR_SYNTAX
16  JSON_ERROR_UTF8

Just to be sure, echo each of the above constants and see what the value is.

And, if my suspicion is correct it is going to be because when you stripslashes
and then try and decode it, it breaks because the escaped characters are not
longer escaped.

My suggestion would be to UTF*_encode() each piece of data before you stuff it
into your json string.  Then one you have built your json string from the
encoded data, run it through json_encode() and you should be fine.

2 pennies for my thoughts please... :)

-- 
Jim Lucas

A: Maybe because some people are too annoyed by top-posting.
Q: Why do I not get an answer to my question(s)?
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[PHP] Malformed UTF-8 Data in JSON

2010-07-14 Thread Dave M G

PHP Users,

I'm decoding some JSON data in PHP to convert it into an array.

However, it's not working, and json_last_error() is returning a value of 
"4", which I believe means "Malformed UTF-8 characters, possibly 
incorrectly encoded".


I try at every turn in every setting to ensure that all my code and 
connections are in UTF-8.


But how can I be sure it's valid UTF-8? I've tried using the PHP command 
utf8_encode() on the string, but that hasn't changed anything.


And can I trust the error message?

Any help or advice would be much appreciated.

By the way, here is the code I'm currently testing with. I'm just 
encoding and then decoding a string right in the PHP just to get it to 
work before I even try getting the data from anywhere else.


$myData ='{"display_name":"Test
Guy","email":"test...@testaddress.com","timeout":"1279145273"}';
$myArray1 = json_encode($myData);
$myArray2 = utf8_encode (stripslashes($myArray1));
$myArray = json_decode($myArray2, true);
$jsonerror = json_last_error();

--
Dave M G

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php