How to send multiple values using curl

Hello Guys i want to know how to post multiple values using curl and how to access this values

$postid[] = $_POST['fields'];
$ch = curl_init(''); 
	curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt ($ch, CURLOPT_POST, 1);
	curl_setopt ($ch, CURLOPT_POSTFIELDS,$postid);
	$hasil = curl_exec ($ch);
	curl_close ($ch);

and i am try to get this data like this

$id = $_POST['postid'];
i also try try this 
$id = $postid[id];

But nothing is working please tell me how to use this

Can you add this in the destination code and see what you’ve got?


but when i submit page post page will not open in curl and when i direct send data without curl everything will work fine

What is in $_POST['fields'] - a single value, an array?

All the sample code I’ve seen sends a single string in key/value format as if it were in a URL, either manually or using http_build_query().

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

Not working Nothing will happened

As above.

its array

This code works for me:

echo "building array";
$fields = array("test" => "el1", "test2" => "el2");
$ch = curl_init('http://localhost/testsite/curldest.php'); 
	curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt ($ch, CURLOPT_POST, 1);
	curl_setopt ($ch, CURLOPT_POSTFIELDS,$fields);
	$hasil = curl_exec ($ch);
	curl_close ($ch);

The destination code is

echo "In second code\n";
echo $_POST["test"] . " " . $_POST["test2"];

When run I get

building arrayarray (size=2)
  'test' => string 'el1' (length=3)
  'test2' => string 'el2' (length=3)

string 'In second code
el1 el2' (length=22)

So the output from the second page is returned in your $hasil variable. Not sure exactly how your array is laid out, but if you var_dump([$_POST]) in the destination code it should show you how it’s accessed.

(ETA - works without the http_build_query() function as well, so I’ve taken it out).

Thanks Working great

Good to know.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.