【问题标题】:Transfer array from one page to another using CURL使用 CURL 将数组从一页传输到另一页
【发布时间】:2016-02-04 19:43:07
【问题描述】:

我正在尝试在两个文件之间传输一组数据。

sender.php代码(使用POST方法发送数组的文件)

$url = 'http://localhost/receiver.php';
$myvars = array("one","two","three")
$post_elements = array('myvars'=>$myvars);
$ch = curl_init( $url );
curl_setopt( $ch, CURLOPT_POST, 1);
curl_setopt( $ch, CURLOPT_POSTFIELDS, $post_elements);
curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt( $ch, CURLOPT_HEADER, 0);
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1);

$response = curl_exec( $ch );

echo "$response";

receiver.php 代码(文件从 sender.php 文件接收数组,然后获取数组的每个元素并回显它,并将其放入一个文档 saved.txt 中。 p>

    echo $_POST($myvars); // To test the output of the received data.

      foreach($myvars as $item) {
       if (!empty($item)) {
        echo $item."<br>";
$myfile = file_put_contents('Saved.txt', (" Name: ". ($_POST["$item"])) . PHP_EOL , FILE_APPEND);
      }
    }

数组没有被传输到receiver.php 或者我没有捕捉到它。在文档输出中,我只有在变量 $item 的位置,而不是数组的每个元素。

编辑: 在接收文件中添加了以下代码,以便从内部获取数组元素,但我得到的只是打印出来的数组:

foreach( $_POST as $stuff ) {
    if( is_array( $stuff ) ) {
        foreach( $stuff as $thing ) {
            echo $thing;
        }
    } else {
        echo $stuff;
    }
}

通过在接收文件中添加以下内容:

echo "<pre>";
print_r($_POST);
echo "</pre>";

我得到以下信息:

Array
(
    [myvars] => Array
)

【问题讨论】:

  • 要查看接收器中的帖子内容,请尝试 var_dump($_POST);而不是 echo $_POST($myvars);您将能够看到它是否成功。
  • $_POST($myvars) 没有任何意义。 $_POST 不是一个函数!此外,$myvars 很可能在第二个脚本中没有细化。而foreach($myvars... 的问题是,它与发布的值无关,即使 if $_myvars 已在某处定义。
  • 放置一个标量数组(带有数字键的数组)很少是一个好主意。使用关联数组 (array("value1"=&gt;"one","value2"=&gt;"two","value3"=&gt;"three")) 或 json_serialize() 数组...
  • @arkascha 为了捕获 $_POST($myvars) 发送的数据,我不需要在其中发送变量吗?谢谢我不知道这个。我必须声明 $myvars 变量吗?它不会自动声明,因为它被转移到新页面?将它作为普通数组值传递有什么缺点?
  • 停下!你把事情搞混了。 $myvars没有被转移!被传输的是该变量的值,而不是变量本身。这就是为什么$myvars 没有在接收方定义。正如上面@Crawdingle 所提到的:转储$_POST 的内容以查看 传输的内容。再说一遍:$_POST(...) 没有任何意义,$_POST 不是 函数。你不能像那样使用那个标识符,它是一个array。所以你必须使用方括号:$_POST[...]...

标签: php arrays post curl http-post


【解决方案1】:

好的,上面cmets中讨论的底线导致了这个结果:

发送部分:

<?php
$url = 'http://localhost/out.php';
$myvars = array("one","two","three");
$post_elements = array('myvars'=>$myvars);
$ch = curl_init( $url );
curl_setopt( $ch, CURLOPT_POST, 1);
curl_setopt( $ch, CURLOPT_POSTFIELDS, http_build_query($post_elements));
curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt( $ch, CURLOPT_HEADER, 0);
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec( $ch );
print_r($response);

接收部分:

<?php
print_r($_POST);

发送端的输出为:

Array ( [myvars] => Array ( [0] => one [1] => two [2] => three ) )

这基本上是说你可以简单地在接收端使用$_POST['myvars'],它将准确地保存你想要传输的标量数组。

【讨论】:

    【解决方案2】:

    尝试序列化数组,因为它总是对我有帮助:

    $url = 'http://localhost/receiver.php';
    $myvars = array("one","two","three");
    $myvars_post=join(" ",$myvars);
    $ch = curl_init( $url );
    curl_setopt( $ch, CURLOPT_POST, 1);
    curl_setopt( $ch, CURLOPT_POSTFIELDS, "array=".urldecode($myvars_post));
    curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt( $ch, CURLOPT_HEADER, 0);
    curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1);
    
    $response = curl_exec( $ch );
    
    echo "$response";
    

    并在receiver.php 中使用:

    print_r($_POST);
    

    【讨论】:

    • 感谢您的贡献。可悲的是,我运行了你的代码,但我得到了 array(0) { } 而且我不是 -1 的人,因为我什至还不能投票,只是加入了。
    • 没关系实际上是我的错请检查我添加到代码中的内容
    • 我运行了它,但我得到一个解析错误:语法错误,意外的 T_VARIABLE on: $myvars_post = join(" ",$myvars);
    猜你喜欢
    • 2021-11-12
    • 2018-04-24
    • 1970-01-01
    • 1970-01-01
    • 2017-08-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多