1

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

2
  • 1
    I avoid sending PHP data serialized and opt for JSON instead. You may want to try that instead. Commented Jan 11, 2011 at 18:28
  • I am very unfamiliar with json, though have used it sometimes to deal with an api (mostly as a mysterious blackbox :)). Do u mean I should try json_encode instead of serialize with curl or something completely different. Commented Jan 11, 2011 at 18:42

2 Answers 2

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']);
Sign up to request clarification or add additional context in comments.

3 Comments

which server? the proxy server that has curl script or server that curl is making request to?
also, any idea how to avoid removal of slashes that i dont want. that is - what if the slash is part of data (and not put by server?)
This solved my problem, and most importantly, my doubt that double escaped things will be preserved (on hindsight, i should have figured that out but sometimes just go blank with a simple problem :)
1

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.

4 Comments

thats strange! let me try to debug why it isnt working for me
But if you post it to a server which has magic quotes enabled, it will be escaped again, which will render it unusable without filtering through stripslashes.
this is the error I get with the code you posted: Notice: unserialize() [function.unserialize]: Error at offset 304 of 351 bytes in /var/www/localserver/test/ser.php on line 8
@nikc: this time, I am not posting it, but directly using the php script which sberry2A posted (on the remote server in question)

Your Answer

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