In order to pass array variables via my curl script, I am using serialize because curl POST elements must not be arrays.
The string that I get after serialization is:
a:10:{s:8:"question";s:18:"How are you doing?";s:11:"view_option";s:6:"select";s:10:"txt_answer";a:4:{i:0;s:8:"dsadsdsa";i:1;s:5:"dsads";i:2;s:10:"dsadsdsdsa";i:3;s:0:"";}s:4:"next";s:1:"9";s:7:"bgimage";s:0:"";s:9:"bck_color";s:0:"";s:12:"border_color";s:0:"";s:11:"select_font";s:1:"1";s:9:"font_size";s:4:"12px";s:4:"poll";s:9:"Get Poll!";}
Curl makes it:
a:10:{s:8:\"question\";s:18:\"How are you doing?\";s:11:\"view_option\";s:6:\"select\";s:10:\"txt_answer\";a:4:{i:0;s:8:\"dsadsdsa\";i:1;s:5:\"dsads\";i:2;s:10:\"dsadsdsdsa\";i:3;s:0:\"\";}s:4:\"next\";s:1:\"9\";s:7:\"bgimage\";s:0:\"\";s:9:\"bck_color\";s:0:\"\";s:12:\"border_color\";s:0:\"\";s:11:\"select_font\";s:1:\"1\";s:9:\"font_size\";s:4:\"12px\";s:4:\"poll\";s:9:\"Get Poll!\";}
before sending to the server. Above is what I see at the server end. Now, because of the backslashes, above is not unserializable.
What do I do now? If I just unescape all quotes - how do I distinguish between escapes put by CURL and escapes that could be part of the data?
EDIT
The error I get when trying to unserialize the escaped string is:
Notice: unserialize() [function.unserialize]: Error at offset 304 of 351 bytes in /var/www/localserver/test/ser.php on line 8
thanks
JP
Your server probably has magic quotes enabled, which means that your input data is escaped.
Your options are to disable it in your php.ini file or to call stripslashes on the data when it is received.
Escapes that are part of the data will be double escaped, so unescaping them shouldn't be a problem.
Disabling in php.ini
magic_quotes_gpc = Off
stripslashes
$data = stripslashes($_POST['data']);
As I mentioned in my comments, you may want to try JSON instead. But, wanted to point out that this works fine for me.
<?php
$c = "a:10:{s:8:\"question\";s:18:\"How are you doing?\";s:11:\"view_option\";s:6:\"select\";s:10:\"txt_answer\";a:4:{i:0;s:8:\"dsadsdsa\";i:1;s:5:\"dsads\";i:2;s:10:\"dsadsdsdsa\";i:3;s:0:\"\";}s:4:\"next\";s:1:\"9\";s:7:\"bgimage\";s:0:\"\";s:9:\"bck_color\";s:0:\"\";s:12:\"border_color\";s:0:\"\";s:11:\"select_font\";s:1:\"1\";s:9:\"font_size\";s:4: \"12px\";s:4:\"poll\";s:9:\"Get Poll!\";}";
print_r(unserialize($c));
OUTPUT
Array
(
[question] => How are you doing?
[view_option] => select
[txt_answer] => Array
(
[0] => dsadsdsa
[1] => dsads
[2] => dsadsdsdsa
[3] =>
)
[next] => 9
[bgimage] =>
[bck_color] =>
[border_color] =>
[select_font] => 1
[font_size] => 12px
[poll] => Get Poll!
)
EDIT
As mentioned by #lonesomeday, you probably have php magic quotes turned on on the server receiving this data.
Related
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.
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.
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
)
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.
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.