Jump to content

PHP, Rest Service, Croatian letters as a value of parameters in POST request JSON body


Go to solution Solved by IvanLjubicic,

Recommended Posts

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 code

function file_get_contents_utf8($fn) {
$content = file_get_contents($fn);
return mb_convert_encoding($content, 'UTF-8');
}

 

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.

Edited by jazzman1

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 :)

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!"
Edited by jazzman1
  • Solution

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 :)

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.