【问题标题】:Sending POST with cURL - Empty array使用 cURL 发送 POST - 空数组
【发布时间】:2012-05-03 02:40:22
【问题描述】:

我正在尝试使用 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
?>

【问题讨论】:

  • 您将字段数量count($fields) 发送到CURLOPT_POSTFIELDS 而不是实际字段,这是故意的吗?

标签: php post curl


【解决方案1】:

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

curl_setopt($connection, CURLOPT_POSTFIELDS, $fields);

您实际上没有在任何地方设置 cURL 选项来发送字段

【讨论】:

    猜你喜欢
    • 2013-04-02
    • 2010-11-08
    • 1970-01-01
    • 2022-01-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-05
    相关资源
    最近更新 更多