Php curl and serialize problem

人走茶凉 提交于 2019-12-25 01:48:33

问题


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


回答1:


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']);



回答2:


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.



来源:https://stackoverflow.com/questions/4661168/php-curl-and-serialize-problem

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!