Posting an array with curl_setopt

everactive
everactive used Ask the Experts™
on
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);

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
Please try

curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

change to

curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));

Open in new window

Please use the

$data = implode(',',$data);
 $ch = curl_init();
       
 curl_setopt($ch, CURLOPT_URL, "MY_URL");
 curl_setopt($ch, CURLOPT_POST, 1);
 curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

thanks

Kalpan

I serialized/unserialized the array.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial