tags:

views:

135

answers:

3

Hi,

The attached code is returning "Notice: Array to string conversion in...". Simply my array is being handled to the remote server as a string containing "Array" word. the rest of the variables are fine.

How can I pass my array ($anarray) without this problem?

Thanks.

<?php

$data = array(
    'anarray' => $anarray,
    'var1' => $var1,
    'var2' => $var2
 );

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, "MY_URL");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

curl_exec($ch);

?>
A: 

If $anarray is an array, as I suspect it is, it shouldn't be. Turn it into a string, by concatenating or whatever appropriate method.

Edit: See Eric Butera's answer.

GZipp
A: 

If you serialize your array it will be in string format:

curl_setopt($ch, CURLOPT_POSTFIELDS, serialize($data));
// $data is now: a:3:{s:7:"anarray";N;s:4:"var1";N;s:4:"var2";N;}
// The values of variables will be shown but since we don't have them this is what we get

You then need to unserialize it on the receiving end:

$data = unserialize($_POST['data']);
// $data is an array again.
John Conde
+1  A: 

The best way to accomplish what you're after is to use http_build_query. http://us.php.net/http_build_query

Eric Butera