21

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

?>
John Conde
  • 217,595
  • 99
  • 455
  • 496
Essam
  • 351
  • 1
  • 4
  • 8
  • 1
    see http://stackoverflow.com/questions/3772096/posting-multidimensional-array-with-php-and-curl – Julien Apr 26 '13 at 10:35

5 Answers5

35

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

John Conde
  • 217,595
  • 99
  • 455
  • 496
  • 3
    `serialize` produces data in a completely wrong format for this operation – Pavel Koryagin Jan 07 '12 at 19:36
  • 1
    Why why why, why would you do that? – sepehr Aug 23 '12 at 06:05
  • 2
    http_build_query is fine, those downvotes and comments must be to an older version of the answer – depoulo Apr 28 '14 at 15:35
  • But since it has been edited after there was better answer with `http_build_query`.... – Smar Mar 26 '15 at 11:09
  • It converts some integer to string which is a big problem on my end – PauAI Feb 02 '17 at 04:00
  • ``` curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($postData['data']) ); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1); curl_setopt($ch, CURLOPT_HTTPHEADER, array( "cache-control: no-cache", "content-type: application/json" )); ``` Worked for me – PauAI Feb 02 '17 at 06:27
15

The best way to accomplish what you're after is to use http_build_query().

Smar
  • 8,109
  • 3
  • 36
  • 48
Eric Butera
  • 638
  • 4
  • 6
  • 2
    This way cURL will generate a `application/x-www-form-urlencoded` HTTP request which is fine unless you want to upload a file that requires a `multipart/form-data` request. – sepehr Aug 23 '12 at 06:07
1

From http://www.php.net/manual/en/function.curl-setopt.php description of CURLOPT_POSTFIELDS

The full data to post in a HTTP "POST" operation. To post a file, prepend a filename with @ and use the full path. The filetype can be explicitly specified by following the filename with the type in the format ';type=mimetype'. This parameter can either be passed as a urlencoded string like 'para1=val1&para2=val2&...' or as an array with the field name as key and field data as value. If value is an array, the Content-Type header will be set to multipart/form-data. As of PHP 5.2.0, value must be an array if files are passed to this option with the @ prefix.

Tim Dearborn
  • 1,178
  • 7
  • 18
1

Due to the nature of the HTTP protocol, and the way curl_setopt function works, $anarray cannot be passed directly as an array.

The following statement:

curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

takes an array of POST parameters and for each of them there must be a string name and a STRING value. You are passing an array value instead, so the PHP processor is forced to convert it to a string using some lame built-in algorithm, which incurs issuance of the before-mentioned notice ("Array to string conversion in...").

So, in order to properly pass that array ($anarray) to the other side, you have to take care of its encoding (into a string) yourself, as well as the other side has to take care of its decoding (from a string).

My approach in such situations is JSON. It is suitable enough in almost all cases. All you have to do is apply the following technique:

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

And then, on the other side of the connection you would retrieve the original array the following way:

$anarray=json_decode($_POST['anarray'],true); // true indicates for associative array rather than an object
0

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
  • 5,386
  • 1
  • 22
  • 18
  • I agree but what is missing the OP's question is also how is the data processed on the remote server. Without this information one cannot possibly say that json_encode() or serialize() or something else is the way to go. – Iam Zesh Aug 21 '13 at 08:57