PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
$string = "Éggs"; // UTF-8 encoding string (extracted from http post data in reality)
$json = json_encode($string);
// At this point $json == "\u00c9ggs"
I have also tried using Zend_Json::encode however that yields the same results. Anyone know why this would be? I would expect for the string to still read Éggs
That's the ASCII representation of UTF characters, which should be safe for transport. If you run json_decode on it it should come back to the original characters.
The problem is that I am trying to use this in a client/server situation where the client generates an sha1 hash based on the json string it is posting, and the server (php) generates an sha1 hash using the same shared secret as the client in order to ensure that the request is coming from a trusted client. My signature check is failing because I'm getting the ascii representation of the characters. My understanding is that json_encode requires a UTF8 string and returns a UTF8 json string, so why is it returning the ascii representation of those characters? Is there any way to process the json string so that those ascii representations are turned back into their true utf8 chars? I feel like this should not be this hard but have been struggling with this problem for a while. Any help is greatly appreciated!
The client is an objective c application. It takes the json string (which has the "real" characters in the json string, not the ascii-encoded unicode characters) and generates the signature based on that. So the problem is that the objective-c client is signing the request based on the json string:
"Éggs"
and my php application is calculating the signature based on:
"\u00c9ggs"
This obviously generates different sha1 hashes.
From what I've read, it is not required for json libraries to use the "safe" encoding (i.e. ascii-encoded unicode characters) as evidenced by the objective-c json library leaving the É as-is. What is encodeURIComponent()? I don't see that in the php documentation. I am having a hard time believing there is no php function which will convert \u00c9 back to É but I can't seem to find it, try as I might. Is the only solution to try and find a way for the client to generate json using the "safe" encoding method?
I thought by client you meant a web browser. encodeURIComponent() is a javascript function.
Why are you json_encoding a single string? is there a purpose to that? JSON is usually used to transport more complex data structures (such as arrays and objects). A JSON'ed string is still a string (only as you can tell, PHP will transform UTF characters to their ASCII representation)
The string I'm using here is simply for illustrative purposes. In reality it is a data structure which contains strings which may have unicode characters.
The ASCII transformation is for compatibility purposes, due to differing browsers' implementation of JSON. Objective-C doesn't have to deal with browsers (at least directly) so it doesn't care for that. You could submit a feature request to PHP for a parameter indicating ASCII conversion but you're probably short on time. You could write your own alternative to encode the data into JSON, take a look at ready components that do that (such as Zend_Json_Encoder), you could probably reuse most of it and just remove the ASCII encoding.