简体   繁体   English

使用cURL和PHP中的foreach发送多维数组

[英]Sending a multidimensional array using cURL and foreach in php

I read this other question too: Missing index in a multidimensional array PHP 我也阅读了另一个问题: 多维数组PHP中缺少索引

and I searched for missing keys and there are not. 我搜索了丢失的钥匙,但没有。

My goal is to create a csv file with the information inside the arrays. 我的目标是使用数组内部的信息创建一个csv文件。 I'm able to do that when I am processing it in the same server, but due to upgrade in the environment and division of the csv files, I must use a second server therefore the CURL request. 我可以在同一台服务器上处理它,但是由于环境的升级和CSV文件的划分,我必须使用第二台服务器,因此必须使用CURL请求。

The problem is that I am able to call the receiver.php and this, in fact, creates the file.csv , but this file is empty (0 bytes). 问题是,我能够调用receiver.php而这,其实,造成FILE.CSV,但这个文件是空的(0字节)。 And the only Log I have is the one below.. 我唯一的日志是下面的日志。

Please find below the result of the php after it ran: 运行后,请在下面找到php的结果:

 Array ( [0] => Array ( [0] => name [1] => frank ) [1] => Array ( [0] => address [1] => none ) [2] => Array ( [0] => email [1] => some@email ) [3] => Array ( [0] => job [1] => none ) [4] => Array ( [0] => ) [5] => Array ( [0] => options [1] => status [2] => link [3] => time [4] => note [5] => check ) [6] => Array ( [options] => none [status] => active [link] => http://something [time] => 2018-01-11 10:00 [note] => none [check] => OK ) ) Array key exist for 0 Array key exist for 1 Array key exist for 2 Array key exist for 3 Array key exist for 4 Array key exist for 5 Array key exist for 6 

I have a form with many <input> fields; 我有一个包含许多<input>字段的表单; 4 of them are static (name, email etc), the others are generated each time the user presses a button. 它们中的4个是静态的(名称,电子邮件等),其他每个在用户按下按钮时生成的。 At each click a row appears and the user can enter more data. 每次单击都会出现一行,用户可以输入更多数据。

This data's <input name=...> is: 该数据的 <input name=...> 为:

at row0:
name='prefs[0][options]'
name='prefs[0][status]'
name='prefs[0][link]'
name='prefs[0][time]'
name='prefs[0][note]'
name='prefs[0][check]'

If the user clicks the button, the row appears in the table and the new <input> fields will be:
name='prefs[1][options]'
name='prefs[1][status]'
name='prefs[1][link]'
name='prefs[1][time]'
name='prefs[1][note]'
name='prefs[1][check]'

.. and so on.

When the form is submitted, the sender.php processes the data. 提交表单后, sender.php处理数据。

I will have 2 arrays : 我将有2个数组

A - $list, which contain the static fields
B - $_POST['prefs'] containing each row

I merge the 2 arrays together: 我将两个数组合并在一起:

$final_array = array_merge($list, $_POST['prefs']);

The log says : 日志说

PHP Notice:  Undefined index: fields_string in /var/www/report/receiver.php on line 4
PHP Warning:  Invalid argument supplied for foreach() in /var/www/report/receiver.php on line 4

sender.php sender.php

<?php

if (isset($_POST['submit'])){
    require_once('config.php');

    $name = addslashes(strip_tags($_POST["name"]));
    $address = addslashes(strip_tags($_POST["address"]));
    $email = addslashes(strip_tags($_POST["email"]));
    $job = addslashes(strip_tags($_POST["job"]));

    $list = array (
        array('name', "$name"),
        array('address', "$address"),
        array('email', "$email"),
        array('job', "$job"),
        array(''),
        array('options', 'status', 'link','time', 'note','check' )
    );


    // Merge the 2 arrays and split to variables
    $final_array = array_merge($list, $_POST['prefs']);
    echo '<pre>'; print_r($final_array); echo '</pre>';
    sleep(2);

    // Check for missing keys
    $firstkey = key($final_array); // get first index of array
    end($final_array);         
    $lastkey = key($final_array);  // get last index of array
    for($i = $firstkey;$i <= $lastkey;$i++) {
        if(!array_key_exists($i,$final_array)) { // check key exist or not 
            echo "Missing Array key is ".$i."<br/>";
        } else echo "Array key exist for ".$i."<br>";
    }


    $fields_string = "";
    foreach($final_array as $key=>$value) {
        $fields_string .= $key.'='.$value.'&';
        }
    rtrim($fields_string, '&');


    // Send through CURL
    $ch = curl_init('http://192.168.10.10/receiver.php');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);

    // execute!
    $response = curl_exec($ch);

    // close the connection, release resources used
    curl_close($ch);

    // do anything you want with your response
    var_dump($response);


    header("Location:index.php");
    exit;

}
?>

receiver.php receiver.php

<?php
$csv = fopen( "/home/user/file.csv", 'w+' );
foreach ($_POST["fields_string"] as $fields) {
    fputcsv($csv, $fields, ";");
}
fclose($csv);
?>

Thanks to This awesome reply I understood I must use $POST only. 感谢这个真棒的答复,我知道我只能使用$POST

sender : 发件人

<?php

function post_to_url($url, $data) {
    $post = curl_init();

    curl_setopt($post, CURLOPT_URL, $url);
    curl_setopt($post, CURLOPT_POSTFIELDS, http_build_query($data));
    curl_setopt($post, CURLOPT_RETURNTRANSFER, true);

    $result = curl_exec($post);
    curl_close($post);
}

if (isset($_POST['submit'])){

    $name = addslashes(strip_tags($_POST["name"]));
    $address = addslashes(strip_tags($_POST["address"]));
    $email = addslashes(strip_tags($_POST["email"]));
    $job = addslashes(strip_tags($_POST["job"]));

    $list = array (
        array('name', "$name"),
        array('address', "$address"),
        array('email', "$email"),
        array('job', "$job"),
        array(''),
        array('options', 'status', 'link','time', 'note','check' )
    );


    // Merge the 2 arrays together
    $final_array = array_merge($list, $_POST['prefs']);

    // Send through CURL
    post_to_url("http://192.168.10.10/receiver.php", $final_array);

    header("Location:index.php");
    exit;
}

?>

receiver : 接收者

<?php
$csv = fopen( "/home/user/file.csv", 'w+' );
foreach ($_POST as $fields) {
    fputcsv($csv, $fields, ";");
}
fclose($csv);
?>

And now it works. 现在就可以了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM