Sending POST with cURL - Empty Array

I am trying to send a POST request using cURL, but unfortunately I only get an empty string.

Curl:

<?php
echo "ID: " . $_POST["id"]; // here ID is not empty

$fields = array(
    'id' => urlencode($_POST["id"]),
    'name' => urlencode($_POST["name"])
);

$connection = curl_init();
curl_setopt($connection, CURLOPT_URL, "http://www.example.de/remote.php");
curl_setopt ($connection, CURLOPT_POST, true);
curl_setopt($connection, CURLOPT_POSTFIELDS, count($fields));
curl_setopt($connection, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($connection, CURLOPT_HEADER, 0);
$response = curl_exec($connection);
?>

Remote Server:

<?php
   var_dump($_POST); // shows an empty array
?>
+3
source share
1 answer

You need to remove count($fields)and use instead$fields

curl_setopt($connection, CURLOPT_POSTFIELDS, $fields);

Nowhere do you actually set the cURL option to send fields

+4
source

All Articles