Posting an array with curl_setopt

Essam picture Essam · Jan 28, 2010 · Viewed 37.1k times · Source

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?

<?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);

?>

Answer

John Conde picture John Conde · Jan 28, 2010

Use http_build_query()

curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
// The values of variables will be shown but since we don't have them this is what we get

You can then access it normally using the $_POST superglobal