Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Posting an array with curl_setopt

Tags:

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);  ?> 
like image 452
Essam Avatar asked Jan 28 '10 21:01

Essam


2 Answers

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

like image 166
John Conde Avatar answered Sep 22 '22 22:09

John Conde


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

like image 27
Eric Butera Avatar answered Sep 25 '22 22:09

Eric Butera