cURL / Mailing Lists / curl-and-php / Single Mail

curl-and-php

RE: Issue with sending arrays in CURLOPT_POSTFIELDS

From: Liu Shan Shui <me_at_lx.sg>
Date: Fri, 10 Jul 2009 08:46:43 +0800

Hi Alex,

 

Glad to hear from you again. By the way, you spelled my name wrongly in your
message (it's "Shui", not "Shut").

 

For your case, you can either opt to pass raw POST data directly to
CURLOPT_POSTFIELDS using the php://input stream:

http://www.jellyandcustard.com/2006/01/05/raw-post-data-in-php/

http://phpieceofcake.com/?92183887

http://phpieceofcake.com/?86183958

 

Or, recursively flatten (implode) arrays in the $_POST data:

http://phpieceofcake.com/?38186473

 

function qsImplode($array, $prefix = '') {

                $qs = '';

                foreach ($array as $name => $value) {

                                $absolute_name = $prefix ?
"{$prefix}[$name]" : $name;

                                $qs .= is_array($value) ?

                                                qsImplode($value,
$absolute_name) :

                                                urlencode($absolute_name) .
'=' . urlencode($value);

                                $qs .= '&';

                }

                return strval(substr($qs, 0, -1));

}

$qs = qsImplode($_POST);

 

Let me know if you have any further questions.

 

With regards,

Liu Shan Shui

me_at_lx.sg

"Life would be much easier if I had the source code." - Anonymous

 

From: curl-and-php-bounces_at_cool.haxx.se
[mailto:curl-and-php-bounces_at_cool.haxx.se] On Behalf Of Alex Judd
Sent: Thursday, July 09, 2009 1:15 AM
To: curl-and-php_at_cool.haxx.se
Cc: 'Alex Judd'
Subject: Issue with sending arrays in CURLOPT_POSTFIELDS

 

Hi everyone

 

Thanks for the reply to my earlier Curl Proxy Cookies question - got that
all working great thanks to Liu Shan Shut!

 

I'm having issues sending Arrays in CURLOPT_POSTFIELDS and wondered if there
was any advice anyone had on this?

 

Basically I have an array of radio boxes, that POST to Curl for it to proxy,
and in my POST -> CURLOPT_POSTFIELDS function, I try and flatten them with
$key = $value&$key2 = $value2 etc.

 

Arrays however come in as

 

$key = 'key' and

$value = Array

 

And this gets rejected (or at least seem to be) rejected by the server at
the other end which is expecting a 'item.array[array number].value' to come
through.

 

Any ideas as to the best way to deal with this - any advice much
appreciated!!

 

Thanks in advance

 

Alex

 

Skywire | www.skywire.co.uk | alex <mailto:alex_at_skywire.co.uk> at skywire
dot co dot uk

Please don't print this email and help the environment

 

_______________________________________________
http://cool.haxx.se/cgi-bin/mailman/listinfo/curl-and-php
Received on 2009-07-10