NYCPHP Meetup

NYPHP.org

[nycphp-talk] How to pass on variables with POST without using a form

Dan Cech dcech at phpwerx.net
Wed Sep 20 20:04:30 EDT 2006


CED wrote:
> Good thing for HTMLentities eh?

Not really, you don't want to use htmlentities in this situation.  You
are escaping your values into POST data so you should use urlencode, as
in the example below.

> ----- Original Message ----- 
> From: "David Krings" <ramons at gmx.net>
> To: "NYPHP Talk" <talk at lists.nyphp.org>
> Sent: Wednesday, September 20, 2006 7:46 PM
> Subject: Re: [nycphp-talk] How to pass on variables with POST without using a
> form
> 
> 
> Hi,
> 
> indeed cURL it is, but that wasn't what I recall using. I also found the
> deal with sockets, but that looks awfully scary.
> 
> Now, cURL triggers another question. Can I send multiple
> 
>          curl_setopt($ch, CURLOPT_POSTFIELDS, "$curlpost");
> 
> with $curlpost something like data1=value1, or do I need to jam that all
> into one string and tie it together with the &? If yes, then I need to add
> more code to screen for & on all variables as it is very likely that the &
> may be included. I also saw on php.net this:
>          "Just a reminder: When setting your CURLOPT_POSTFIELDS remember to
> replace the spaces in your values with %20 "
> 
> Ah, bummer! It basically expects the name=data sets to be formatted as if
> one wants to send a get via URL.

The easiest way I've found to do this is as follows:

$myvars = array(
  'item1' => 'value1',
  'item2' => 'value2',
);

$postdata = array();
foreach ($myvars as $k => $v) {
  $postdata[] = urlencode($k) .'=' . urlencode($v);
}
$postdata = implode('&',$postdata);

curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);

By running both your keys and values through urlencode, you'll be sure
that you won't end up with mangled post data, no matter what the values'
contents are.

If you want to be really ahead of the curve you can use
ini_get('arg_separator.output') in place of '&' in the implode.

Dan

> 
> I guess I go against my own advice, pack the two dozen variables into the
> session and then unset them after savely retrieving them........unless
> someone has a better idea that is not as painful as manipulating headers or
> massaging strings for cURL to use.
> 
> Bah! This rapidly inhales! :(
> 
>                  Thanks for the tip anyway, I can see some other good uses
> for cURL.
> 
> 
>                          David K.



More information about the talk mailing list