使用cURL发送POST - 空数组 [英] Sending POST with cURL - Empty array

查看:784
本文介绍了使用cURL发送POST - 空数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用cURL发送POST请求,但很遗憾,我只接收到一个空字符串。

I'm trying to send a POST request with cURL, but unfortunately I'm only receiving 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);
?>

远程服务器:

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


推荐答案

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

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

curl_setopt($connection, CURLOPT_POSTFIELDS, $fields);

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

Nowhere do you actually set the cURL option to send the fields

这篇关于使用cURL发送POST - 空数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆