/    Sign up×
Community /Pin to ProfileBookmark

Tricky one: post a string to be recognized as array…

Hi,

For some hard-to-describe reason, I would need to post (using curl and post) a string so that it is recognized as an array by the target site. I cannot directly post an array. So I am trying the following :

[code=php]$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,”http://www.bla.com”);
curl_setopt($ch, CURLOPT_POST, 1);

$all=”Array”.”rn”.”(“.”rn”.”[name] => paul”.”rn”.”)”.”rn”;

curl_setopt($ch, CURLOPT_POSTFIELDS, $all);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result=curl_exec ($ch);
curl_close ($ch);
print $result; [/code]

Somehow, this does not work (I do not know how to post as an array…). Can anyone help me with this rather unusual quest ?

Many thanks in advance !!!

to post a comment
PHP

13 Comments(s)

Copy linkTweet thisAlerts:
@StevishMar 15.2009 — I had this same issue, and I used serialize() and unserialize(). Due to magic quotes being on, I had to use unserialize(stripslashes($whatever)). Serialize() turns any kind of variable into a string, and unserialize() turns it back into a variable.

But in the end, I gave that up and used $_SESSION variables to pass the array directly. But as long as there are no line breaks and limited special characters in your data, serialize may work for you.
Copy linkTweet thisAlerts:
@StevishMar 15.2009 — Ps. In case I wasn't clear, you would use serialize() in the script you gave, and you would use unserialize() in the script at "http://www.bla.com" in your example
Copy linkTweet thisAlerts:
@patimagesauthorMar 15.2009 — thanks a lot for the reply! Unfortunately, I cannot change the target (used for too many other things) and therefore cannot unserialize... Any other idea are welcome as I am fresh out !
Copy linkTweet thisAlerts:
@chazzyMar 15.2009 — the first issue i see is that you need a key/value pair for any data that is posted. it's not enough to do this..

[code=php]
$all="Array"."rn"."("."rn"."[name] => paul"."rn".")"."rn";
curl_setopt($ch, CURLOPT_POSTFIELDS, $all);
[/code]


$all needs to be something like

[code=php]
$all="arrayData=Array"."rn"."("."rn"."[name] => paul"."rn".")"."rn";
[/code]


where arrayData is the request key they're expecting. i think you need to get more information from whoever you're posting this data to, as it sounds like they might be expecting it in a specific format (ie an array in PHP isn't exactly what you need from print_r($array)) are sure they're PHP on the other end?
Copy linkTweet thisAlerts:
@patimagesauthorMar 15.2009 — that s a good point. Basically, I can get it to work with html when I post with "post method" from a form that uses enctype="multipart/form-data", therefore using an array. I should mention the target is a jsp file. I am basically trying to reproduce the same than an html post using "multipart/form-data" with curl. The main problem is to pass the data exactly as html would (unfortunately, posting an array directly does not work for technical reasons)... and the lastest suggestion did not work... any help is greatly appreciated !

PS : one question though, what is "arrayData" in a normal html post ?
Copy linkTweet thisAlerts:
@NogDogMar 15.2009 — Without knowing exactly what the target app is looking for, it's hard to know exactly what to send it. In general, though, you want to send the post data the same as you would get data in a URL. You can either use an array...
[code=php]
$all = array(
'name' => urlencode($name),
'password' => urlencode($password),
'foo' => urlencode($bar)
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $all);
[/code]
...or build the query string yourself:
[code=php]
$all = "name=" . urlencode($name) . "&password=" . urlencode($password) . "&foo=" . urlencode($bar);
curl_setopt($ch, CURLOPT_POSTFIELDS, $all);
[/code]
Copy linkTweet thisAlerts:
@patimagesauthorMar 15.2009 — Thanks Nogdog, but that will not work. The target looks for an array (multipar/data) but the array cannot be sent directly (as proposed in your post). I have to somehow "build it" the same way html would (thereby my idea to encode it). Does anybody know how to build from scratch an array in the way the following would ?

[code=php]<form name="popo" action="t3.php" target="_top" method="post" enctype="multipart/form-data" id="upload">
<table border=0>
<tr>
<td><input type="checkbox" name="showOption" value=1 checked></td>
<td>&nbsp; Origin(s) </td>
</tr>
<tr>
<td><input type="checkbox" name="showOption" value=2 checked></td>
<td>&nbsp; Promoter(s) </td>
</tr>
<tr>
<td><input type="checkbox" name="showOption" value=3 checked></td>
<td>&nbsp; Terminator(s) </td>
</tr>
<tr>
<td><input type="checkbox" name="showOption" value=4 checked></td>
<td>&nbsp; Marker(s) </td>
</tr>
</table>
</form>[/code]


For info, all items have the same name and I cannot change this...
Copy linkTweet thisAlerts:
@chazzyMar 15.2009 — ~snip~[/code][/QUOTE]

multipart/form-data has [b]nothing[/b] to do with it being an array. it's the default form encode if none specified. assuming that what you posted is correct (and you're not unnecessarily obfuscating your code), try this:

[code=php]
$all="showOption=1&showOption=2&showOption=3&showOption=4";
curl_setopt($ch, CURLOPT_POSTFIELDS, $all);
[/code]


the JSP will convert this into an array for you. you're not posting an array, you're sending all the field data. you'd dynamically build that string depending on whether you want 1,2,3 or 4.

in my previous post, arrayData is the attribute name value for some input tag.
Copy linkTweet thisAlerts:
@NogDogMar 15.2009 — You'll probably have to build the string manually, then, using square brackets for the field names:
[code=php]
$all = "showOption[]=1&showOption[]=2&showOption[]=3&showOption[]=4";
curl_setopt($ch, CURLOPT_POSTFIELDS, $all);
[/code]
Copy linkTweet thisAlerts:
@chazzyMar 16.2009 — You'll probably have to build the string manually, then, using square brackets for the field names:
[code=php]
$all = "showOption[]=1&showOption[]=2&showOption[]=3&showOption[]=4";
curl_setopt($ch, CURLOPT_POSTFIELDS, $all);
[/code]
[/QUOTE]


except we now know that he's using JSP on the other end, and JSP doesn't understand a showOption[] request parameter.
Copy linkTweet thisAlerts:
@patimagesauthorMar 16.2009 — thanks guys for your help! The problem is that the target jsp will only accept an array (since it work with multipart/form-data I though this made the array). For instance, only the following would work (it does for other values):

[code=php]curl_setopt($ch, CURLOPT_POSTFIELDS, array("showOption"=>"1", "showOption"=>"2", "showOption"=>"3"));
[/code]


But, as you know, the "showOption" key is overwritten in the array... My liberties are limited with the scripts and I tried a lot unsuccessfully (invert keys and values, showOption[], etc). I am therefore down to try to mimic an html post which is the only thing working (through an array using curl).
Copy linkTweet thisAlerts:
@chazzyMar 16.2009 — ...[/QUOTE]

So before I assume the worst, have you tried the suggestions that either NogDog or I just gave, or are you just assuming that it won't work?

Assuming that the HTML form you just posted [url=http://www.webdeveloper.com/forum/showpost.php?p=988999&postcount=8]here[/url] is in fact what works, the http post data includes the string that I posted when sending to a JSP page. I confirmed this using the webdeveloper extension on firefox.
Copy linkTweet thisAlerts:
@patimagesauthorMar 16.2009 — I did try and was not successful... Cannot figure why. It may help to know that the jsp proceeds the "same name" values using multi.getParameterValues.

Can you post here what/how you tried to pass array data made in a string and how you collected the information ?
×

Success!

Help @patimages spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...