cakephp sending encrypted data to library from Controller - php

While working with the cakephp, I found an issue mentioned below.
Fetched the encrypted field info from DB (encrypted using Security::rijndael)
Passed this whole data as an array format to the custom Library(Own created lib).
When i echoed the data in lib as well in controller I amazed to see the result. The value (encrypted one) are showing blank in the lib. Is I missed anything in codding? I searched on google but didn't get the satisfactory answer, Please help me out. Your help will really be appreciated.
Here is result i am getting in controller and Library respectively
Array
(
[0] => Array
(
[value] => s�i�(�RTf���cBЉF� | �r�n#ô�
)
)
Array
(
[0] => Array
(
[value] =>
)
)

Check your character encoding; a place I worked at ran into a similar issue and it was due to our db trying to encode characters it did not support. UTF-8 generic is a, well, generic encoding type.

Related

How to tidy up custom fields inputs in Wordpress

Hi all looking for a little help.
I've created a site which shows the previous form of soccer teams like so:
This works fine and each letter is output by PHP as an image.
The problem is that the only way I could get my head round it to work it out was to create a custom field of checkboxes in Wordpress like so:
What would probably work better would just to have a textbox on the backend where I could just type in the form like "WLDWW" and then the front end display as necessary.
Problem is that I'm not entirely sure where to start with PHP for it to read each individual letter that I put into the textbox and translate that into the image needed at the front end.
Any ideas?
Thanks in advance
Take a look at str_split(). (see here)
$string = "WLWLLW";
$result = str_split($string);
This will output:
Array
(
[0] => W
[1] => L
[2] => W
[3] => L
[4] => L
[5] => W
)
Then you can iterate through the array and display as needed, if you want to use PHP. Of course, I don't know how you've implemented it exactly or how it uses Wordpress, so you may have to make some adjustments as needed.

How to convert an array in PHP into a valid JSON request and send it to web service URL in following scenario?

I've got following array titled $val in PHP :
Array
(
[page_id] => 208
[invite_emails] =>
[invite] => Array
(
[0] => 970
[1] => 991
[2] => 992
)
)
I only want to convert the above array into a valid JSON request and send it to some web service URL.
How should I do it? Please help me.
Thanks in advance.
Your question is rather broad, but to convert a php array into a JSON object is very easy.
$jsonString = json_encode($array);
As for sending it to the URL, this question contains some good info.
Or if you want to use curl, this resource is pretty good.
As others have posted, use the following to json encode your array:
json_encode($val);
Now, your problem is sending this to your web service via parameter. The way I generally send complex parameters via GET parameters is by base64 encoding.
$param = base64_encode(json_encode($val));
Now you can send $param just like any other parameter. Take a look at Guzzle for making HTTP requests from PHP.

display data from an array in json form

i got an array from echo $posts
Array
(
[0] => Array
(
[id] => 14
[name] => اسطنبوليه
)
)
i have this array (part of the main array)and now i wish to convert it to json form. however i am not able to do so, i tried to convert the data through
echo json_encode ($posts);
but instead of original data i am getting an output u0627u0633u0637u0646u0628u0648u0644u064au0647n
can anyone tell how i can get the correct form
Please try echo json_encode($posts,JSON_UNESCAPED_UNICODE) (php 5.4 and above)
2 things. Firstly view source of the output in the browser and you should see it as a JSON encoded string rather than the interpreted version.
Secondly it looks like there is some character encoding issues as the string you're getting back is unicode. Make sure you have the encoding set right on your server and browser.

How to read data from this json format

I am stuck on this. This is a json_encoded string by the Wordpress plugin and saved into database.
I want to read it from my own database query. I am getting null when tried with var_dump .
It has some properties of code which is creating problem, I think.
Below is the data where from i want to read usable data for my use. I am using PHP and Mysql.
a:3:{i:0;O:27:"WpProQuiz_Model_AnswerTypes":7:{s:10:"*_answer";s:3:"100";s:8:"*_html";b:0;s:10:"*_points";i:1;s:11:"*_correct";b:0;s:14:"*_sortString";s:0:"";s:18:"*_sortStringHtml";b:0;s:10:"*_mapper";N;}i:1;O:27:"WpProQuiz_Model_AnswerTypes":7:{s:10:"*_answer";s:3:"200";s:8:"*_html";b:0;s:10:"*_points";i:1;s:11:"*_correct";b:0;s:14:"*_sortString";s:0:"";s:18:"*_sortStringHtml";b:0;s:10:"*_mapper";N;}i:2;O:27:"WpProQuiz_Model_AnswerTypes":7:{s:10:"*_answer";s:8:"Infinite";s:8:"*_html";b:0;s:10:"*_points";i:1;s:11:"*_correct";b:1;s:14:"*_sortString";s:0:"";s:18:"*_sortStringHtml";b:0;s:10:"*_mapper";N;}}
I know this looks nasty but copy and try to decode it.
Ohh In Wordpress you can do as follows
$serialized = 'a:3:{i:0;s:5:"examp";i:1;s:6:"exampl";i:2;s:6:"examp2";}';
var_dump(unserialize($serialized));
Array
(
[0] => examp
[1] => exampl
[2] => examp2
)

json_encode problem

This has got me completely stumped:
print_r($json);
echo json_encode($json);
output:
Array
(
[query] => dia
[suggestions] => Array
(
[0] => Diana Johnson
[1] => Diane Abbott
)
)
{"query":"dia","suggestions":[null,null]}
What on earth is going wrong?
edit Just to add to the general wtf-ery of this, here's another sample:
Array
(
[query] => david
[suggestions] => Array
(
[0] => David Cameron
[1] => David Amess
[2] => David Anderson
[3] => David Blunkett
[4] => David Burrowes
)
)
{"query":"david","suggestions":["David Cameron",null,null,null,null]}
I'm posting this as an answer because I need the full formatting abilities of the normal answer box.
Yeah, it's UTF-8 all right. From the PHP interactive prompt:
php > $david = urldecode('David%A0Amess');
php > echo json_encode($david);
null
php > $david = urldecode('David%20Amess');
php > echo json_encode($david);
"David Amess"
php > $david = urldecode('David%c2%a0Amess');
php > echo json_encode($david);
"David\u00a0Amess"
So, we can assume that you're dealing with either ISO-8859 or Windows-1252, given that we're dealing with a broken NBSP. We can fix this with iconv:
php > $david = urldecode('David%A0Amess');
php > $david_converted = iconv('Windows-1252', 'UTF-8', $david);
php > echo json_encode($david_converted);
"David\u00a0Amess"
So, this means that you are going to need to not trust what you're pulling out of MySQL, assuming you've done the SET NAMES thing. Clearly something has gone awry when you were inserting data. You probably weren't giving MySQL well-formed UTF-8, and it stupidly did not complain. (If you were using other, smarter, more correct databases, and tried to insert the unencoded NBSP, they would have rejected the input.)
This looks like an autocomplete script. I assume your results are loaded from a database, are you sure they're utf-8? If you cannot replicate this functionality by hardcoding the array, then it's probably an encoding issue.
According to http://php.net/manual/en/function.json-encode.php, "This function only works with UTF-8 encoded data."
You can also use http://php.net/manual/en/function.json-last-error.php to see the last error.

Categories