2

I read this other question too: Missing index in a multidimensional array 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. 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.

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). And the only Log I have is the one below..

Please find below the result of the php after it ran:

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; 4 of them are static (name, email etc), the others are generated each time the user presses a button. At each click a row appears and the user can enter more data.

This data's <input name=...> is:

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.

I will have 2 arrays:

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

<?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

<?php
$csv = fopen( "/home/user/file.csv", 'w+' );
foreach ($_POST["fields_string"] as $fields) {
    fputcsv($csv, $fields, ";");
}
fclose($csv);
?>
Sam
  • 16,355
  • 6
  • 53
  • 89
aPugLife
  • 869
  • 1
  • 13
  • 23
  • fields_string is string type you cant use it in foreach. its array function – NanThiyagan Jan 03 '18 at 16:27
  • Why not use `http_build_query($final_array)` ? – Kisaragi Jan 03 '18 at 16:30
  • haven't tried, i will check it tomorrow at the office! thanks – aPugLife Jan 03 '18 at 23:03
  • I used [this](https://stackoverflow.com/questions/8170306/http-build-query-with-same-name-parameters) link: and updated the code: `$array_final = http_build_query_same_names($final_array); curl_setopt($ch, CURLOPT_POSTFIELDS, $array_final);` but i did not solve. Same log error. May I ask you if you have time to provide a solution? I'm kinda confused here – aPugLife Jan 04 '18 at 08:49
  • `curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($final_array));` also trying this way.. I'm wondering if the problem is instead in the receiver... – aPugLife Jan 04 '18 at 13:39

1 Answers1

-1

Thanks to This awesome reply I understood I must use $POST only.

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.

aPugLife
  • 869
  • 1
  • 13
  • 23
  • If I'd want to send a $_GET too, then: `function post_to_url($url, $data, $foo)` - `curl_setopt($post, CURLOPT_URL, $url.'?bar='.$foo);` - `post_to_url("http://192.168.10.10/receiver.php", $final_array, $foobar);` - And the receiver.php: `$foo= $_GET['bar'];`. Easy – aPugLife Jan 05 '18 at 08:48
  • I don't understand why someone downvoted this if the solution fits my needs in total. – aPugLife Jan 10 '18 at 12:00