1

I have created an array in my codeigniter controller and encoded as json as follows :

$templateText = $this->input->post('sampletext');
$template_sample = $this->input->post('sampletext');
$postdata = array('userid'          => LOGIN_FOR_TEMPLATE,
                    'password'          => PASWD_FOR_TEMPLATE,
                    'template'          => $templateText,
                    'template_sample'   => $template_sample,
                    'callback_url'      => base_url().'API/templateCallback'
                ); 

$postdata = json_encode($postdata); 
print_r($postdata); 

if the $templateText is a unicode string, then the array is not encoded properly.

To test this I set this variable as :

$templateText = 'नये ऑफर 123';
$template_sample = 'नये ऑफर 123';

but my print_r() function display details as follows :

{"userid":"xxxx","password":"xxxx","template":"\u0928\u092f\u0947 \u0911\u092b\u0930 123","template_sample":"\u0928\u092f\u0947 \u0911\u092b\u0930 123","callback_url":"http:\/\/example.com\/API\/templateCallback"}

But I need to set it as 'नये ऑफर 123 like :

{"userid":"xxxx","password":"xxxx","template":"नये ऑफर 123","template_sample":"नये ऑफर 123","callback_url":"http://example.com/API/templateCallback"}

How do I solve this

I already set <meta charset="utf-8"> in my html

1
  • That's a fairly strange need... Commented Oct 27, 2017 at 10:52

1 Answer 1

2

Use it as

$postdata = json_encode($postdata, JSON_UNESCAPED_UNICODE); 

You can use it as below for your URL

$postdata = json_encode($postdata,JSON_UNESCAPED_SLASHES|JSON_UNESCAPED_UNICODE); 
Sign up to request clarification or add additional context in comments.

1 Comment

I need one more help. how can i set url like "callback_url":"http://example.com/API/templateCallback" in this json

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.