2

I try to send a multi array via cURL but I can't find a nice way to do it.

My code example:

$data = array( 'a' => 'testa', 'b' => 'testb', 'c[d]' => 'test1', 'c[e]' => 'test2' );

curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

This will work and give the result I want on the curl_init() site:

print_r( $_POST ) :

Array (

    [a] => testa
    [b] => testb
    [c] => Array (

            [d] => test1
            [e] => test2
    )
)

I'd like to add the c array dynamically like:

$c['d'] = 'test1';
$c['e'] = 'test2';

But if I try to add an array with array_push or [] I always get and (string)Array in the Array without data.

Can anyone help me to do it?

The whole code for faster testing:

$url = 'url_to_test.php';
$data = array( 'a' => 'testa', 'b' => 'testb', 'c[d]' => 'test1', 'c[e]' => 'test2' );
$ch = curl_init($url);
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$buffer = curl_exec ($ch);
curl_close ($ch);

echo $buffer;

The test.php

print_r($_POST);

Thanks for any help!

cheers

Thusitha Sumanadasa
  • 1,669
  • 2
  • 22
  • 30
Talisin
  • 614
  • 1
  • 6
  • 17

2 Answers2

4

In

$data = array( 'a' => 'testa', 'b' => 'testb', 'c[d]' => 'test1', 'c[e]' => 'test2' )

You've simply added new string keys "c[d]" and "c[e]".

If you want a nested array, use:

$data = array( 'a' => 'testa', 'b' => 'testb', 'c' => 
    array( 'd' => 'test1', 'e' => 'test2' )
)

-- EDIT --

You're trying to set POST data, which is essentially a set of key value pairs. You can't provide a nested array. You could, however, serialize the nested array and decode it at the other end. Eg:

$post_data = array('data' => serialize($data));

And at the receiving end:

$data = unserialize($_POST['data']);
Hamish
  • 22,860
  • 8
  • 53
  • 67
  • yep and the result is the array string: Array ( [a] => testa [b] => testb [c] => Array ) I cant access the c array – Talisin Sep 22 '11 at 01:01
  • Ah, the POST data has to be key value pairs, it won't take nested array. will edit. – Hamish Sep 22 '11 at 01:09
  • Ive tried it with serialize but with unserialize I get a false back which means 'In case the passed string is not unserializeable, FALSE is returned and E_NOTICE is issued.' This is the string before I try to unserialize: a:3:{s:1:\"a\";s:5:\"testa\";s:1:\"b\";s:5:\"testb\";s:1:\"c\";a:2:{s:1:\"d\";s:5:\"test1\";s:1:\"e\";s:5:\"test2\";}} – Talisin Sep 22 '11 at 01:56
  • Ok found my problem.... need to stripslashes otherwise it can't get unserialize: $data = unserialize(stripslashes($_POST['data'])); – Talisin Sep 22 '11 at 02:05
0

here is your solution

$urltopost = "http://example.com/webservice/service.php";
$datatopost = array (0 =>array('a'=>'b','c'=>'d'),1 =>array('a'=>'b','c'=>'d'),2 =>array('a'=>'b','c'=>'d'),3 =>array('a'=>'b','c'=>'d'));
$post_data = array('data' => serialize($datatopost));

$ch = curl_init ($urltopost);
curl_setopt ($ch, CURLOPT_POST, true);
curl_setopt ($ch, CURLOPT_POSTFIELDS,$post_data);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
$returndata = curl_exec ($ch);

echo "<pre>";
print_r(unserialize($returndata));

service.php code

$temp = unserialize($_POST['data']);
echo serialize($temp);