【问题标题】:How do I send empty array value using PHP in cURL request?如何在 cURL 请求中使用 PHP 发送空数组值?
【发布时间】:2016-07-12 06:04:38
【问题描述】:

在 JAVA 中内置了一个 REST API,它接受一个空数组作为请求参数。如果没有此参数,API 将返回空响应。我不知道发送这个空参数的意义,因为我没有构建这个 API。请求应该是这样才能从 API 获得正确的响应。

//Post data 
$data = array('fieldValues'=>array(),'token'=>'XXXxXX7.......');

$postUrl = 'http://<REST API URL>/<service url>';
$postdata = http_build_query(json_decode(file_get_contents("php://input"), true));

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $postUrl);
curl_setopt($ch, CURLOPT_POST, 1);

// in real life you should use something like:
 curl_setopt($ch, CURLOPT_POSTFIELDS, 
          $postdata);

// receive server response ...
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$server_output = curl_exec ($ch);

curl_close ($ch);
echo $server_output;
exit;

我的问题是 http_build_query 正在从 CURL 帖子字段中转义 fieldValues。如何使其包含在帖子数据中?

【问题讨论】:

    标签: php rest post curl


    【解决方案1】:

    显然http_build_query 去除了空数组。您可以重新实现该函数以包含空数组,也可以手动将空数组添加到发布数据中。

    【讨论】:

      猜你喜欢
      • 2018-03-30
      • 1970-01-01
      • 2013-12-09
      • 1970-01-01
      • 2012-10-13
      • 1970-01-01
      • 1970-01-01
      • 2020-10-10
      • 2021-12-03
      相关资源
      最近更新 更多