【问题标题】:Send whole array as one post parameter using CURL使用 CURL 将整个数组作为一个 post 参数发送
【发布时间】:2013-07-12 10:31:51
【问题描述】:

我有数组,我需要使用 post 请求发送这个数组。

我将此数组序列化为字符串,我需要将整个数组作为一个后置参数发送。

我用于这个 CURL,这是 index.php 代码:

  $name = array(2,3,"5.5");

  $name  = serialize($name);

  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, 'http://site.com/retrieve_post.php');
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($ch, CURLOPT_POST, true);
  curl_setopt($ch, CURLOPT_POSTFIELDS, 'name='.$name);
  curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
  curl_setopt($ch, CURLOPT_HTTPHEADER, array('User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:19.0) Gecko/20100101 Firefox/19.0') );
  $data = curl_exec($ch);
  curl_close($ch);

这是retrieve_post.php文件:

$arr = unserialize($_POST['name']);

$hand = fopen("test.txt", "w+");

if (is_array( $arr )) {
    fwrite($hand, "This is array");
}
else {
    fwrite($hand, "Not array");
}
fclose($hand);

问题是,在 unserialize($_POST['name']) 之后,我得到的不是数组。

test.txt 文件中写入:“不是数组”。

为什么,$arr 没有数组类型?我哪里错了?

【问题讨论】:

  • 您介意print_r($_POST) 到屏幕或文件吗?

标签: php post curl


【解决方案1】:

你可以试试implode,而不是序列化。

$name = implode(',', $name);

您可以在另一个文件中尝试explode

$arr = explode(',', $_POST['name']);

【讨论】:

  • 我需要发送多维数组,所以在这种情况下内爆不是最佳变体...
猜你喜欢
  • 2013-09-14
  • 2016-06-23
  • 1970-01-01
  • 2014-01-30
  • 2012-05-03
  • 1970-01-01
  • 2022-12-03
  • 1970-01-01
  • 2013-04-07
相关资源
最近更新 更多