使用cURL发送POST - 空数组

时间:2012-04-22 01:03:52

标签: php post curl

我试图用cURL发送POST请求,但不幸的是我只收到一个空字符串。

卷曲:

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

远程服务器:

<?php
   var_dump($_POST); // shows an empty array
?>

1 个答案:

答案 0 :(得分:4)

您需要移除count($fields),而只需使用$fields

curl_setopt($connection, CURLOPT_POSTFIELDS, $fields);

您实际上没有设置cURL选项来发送字段