IvanLjubicic Posted February 4, 2014 Share Posted February 4, 2014 Hi guys! I asked on StackOverflow, nobody seems to know the solution, so I'm asking here. I have a problem with my PHP MVC. I'm trying to send some values of JSON parameters (in HTTP POST request body) that include Croatian signs like ŠČĆŽĐŽ. But on the PHP Controller side, when I echo JSON body of HTTP request, I'm getting ? (question marks) instead of those letters (I'm using file_get_contents('php://input') I assume the problem is in that function. I tried absolutely everything I found on Internet, and it didn’t help. When I'm getting some values from MySQL database that include those letters, and echo it in PHP, everything is ok. Here is the part of code. I commented some code that I tried before. Thank you all. //header('Content-Type: text/html; ISO-8859-1');mb_language('uni');mb_internal_encoding('UTF-8');if ($_SERVER['REQUEST_METHOD'] === 'POST') {// get tag//$opts = array('http' => array('header' => 'Accept-Charset: UTF-8'));//$context = stream_context_create($opts);inputJSON = file_get_contents('php://input');echo utf8_encode($inputJSON);echo $inputJSON;//$fileEndEnd = mb_convert_encoding($inputJSON, 'UTF-8', 'UTF-16'); //$fileEndEnd = htmlentities($inputJSON, 'UTF-8', 'UTF-8');//$string = http_get_request_body ();//$string = iconv('UTF-8', 'ASCII//TRANSLIT', $inputJSON);$fileEndEnd = mb_convert_encoding($inputJSON, 'UTF-8',mb_detect_encoding($inputJSON, 'UTF-8, ISO-8859-1', true));//echo $fileEndEnd;echo $fileEndEnd;//echo $string;$input= json_decode($inputJSON);//some codefunction file_get_contents_utf8($fn) {$content = file_get_contents($fn);return mb_convert_encoding($content, 'UTF-8');} Link to comment https://forums.phpfreaks.com/topic/285941-php-rest-service-croatian-letters-as-a-value-of-parameters-in-post-request-json-body/ Share on other sites More sharing options...
jazzman1 Posted February 4, 2014 Share Posted February 4, 2014 I think the answer is very simple here, but who knows On the top of the file try to set apache headers by using a php header() function with utf8 charset. Try, header('Content-Type: application/json; charset=utf-8'); No need to convert everything from one encoding to another. Link to comment https://forums.phpfreaks.com/topic/285941-php-rest-service-croatian-letters-as-a-value-of-parameters-in-post-request-json-body/#findComment-1467731 Share on other sites More sharing options...
IvanLjubicic Posted February 4, 2014 Author Share Posted February 4, 2014 I think the answer is very simple here, but who knows On the top of the file try to set apache headers by using a php header() function with utf8 charset. Try, header('Content-Type: application/json; charset=utf-8'); No need to convert everything from one encoding to another. Unfortunately, I tried that already, and it didn't help. header('Content-Type: application/json; charset=utf-8'); if ($_SERVER['REQUEST_METHOD'] === 'POST') { $inputJSON = file_get_contents('php://input'); $headers = apache_request_headers(); foreach ($headers as $header => $value) { echo "$header: $value <br />\n"; } It echoes that:...Content-Type: application/json; charset=UTF-8 I have another solution to avoid using those kind of characters (by sending parameters without it), but this really pisses me off. Thanks anyway Link to comment https://forums.phpfreaks.com/topic/285941-php-rest-service-croatian-letters-as-a-value-of-parameters-in-post-request-json-body/#findComment-1467743 Share on other sites More sharing options...
jazzman1 Posted February 4, 2014 Share Posted February 4, 2014 I don't have any problem with utf8 encoding charset and php://input The format of output is "text/plain", but there is no problem if it is "json" as well. So, this is my simple test using bulgarian (cyrillic) characters. <?php if (isset($_POST['Submit'])) { if ($_SERVER['REQUEST_METHOD'] === 'POST') { $string = 'Content-Type: text/plain; charset=utf-8'; header($string); $rawPostData = file_get_contents('php://input'); var_dump(rawurldecode($rawPostData)); exit; } } ?> <form method="post" action="#" accept-charset="utf-8"> Request: <input type="text" name="request" /><br> <input type="submit" name="Submit" value="Go!" /> </form> Result: string(33) "request=джазмен&Submit=Go!" Croatian is good to: string(31) "request=ŠČĆŽĐŽ&Submit=Go!" Link to comment https://forums.phpfreaks.com/topic/285941-php-rest-service-croatian-letters-as-a-value-of-parameters-in-post-request-json-body/#findComment-1467764 Share on other sites More sharing options...
IvanLjubicic Posted February 11, 2014 Author Share Posted February 11, 2014 I managed to solve this problem. The problem was in REST client I used for testing https://code.google.com/p/rest-client/ When I called web services with this client, PHP missinterprets Croatian signs (question mark instead of characters). When I called services from Android, everything was ok. Thank you jazzman1, Your posts gave me a hint Link to comment https://forums.phpfreaks.com/topic/285941-php-rest-service-croatian-letters-as-a-value-of-parameters-in-post-request-json-body/#findComment-1468486 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.