PHP错误处理通过电子邮件导入.csv

PHP错误处理通过电子邮件导入.csv

问题描述:

I am importing a .csv file with users once a day.
Now I want to add errorhandling if the file can't be imported.
The thing is that I would like to have all the errors during the proces being collected and mailed to me.

If the script encounters an error it shouldn't stop but continue with the rest of the .csv file.

The only thing I have now is a check if the file excists but I'm currently working on the errorhandling part.

Can anyone telle me how to collect and mail the errors in stead of displaying them?

This is what I have sofar...

<?php
    $filepath = get_bloginfo('template_directory')."/import.csv";
    setlocale(LC_ALL, 'nl_NL');
    ini_set('auto_detect_line_endings',TRUE);
    $file = fopen($filepath, "r") or die("Error opening file");
    $i = 0;

    while(($line = fgetcsv($file, 1000, ";")) !== FALSE) {
        if($i == 0) {
            $c = 0;
            foreach($line as $col) {
                $cols[$c] = $col;
                $c++;
            }
        } else if($i > 0) {
            $c = 0;
            foreach($line as $col) {
                $data[$i][$cols[$c]] = $col;
                $c++;
            }
        }
        $i++;
    }
    echo '<pre>';
    print_r($data);
    echo '</pre>';
    foreach ($data as $gebruiker){
        $username = $gebruiker['username'];
        if ( username_exists( $username ) && strtolower($gebruiker['status']) == 'published' ){
            // IF USER EXISTS: UPDATE
            $user = get_user_by( 'login', $username);
            update_user_meta( $user->ID, 'first_name', $gebruiker['first_name'] );
            update_user_meta( $user->ID, 'surname_prefix', $gebruiker['surname_prefix'] );
            update_user_meta( $user->ID, 'last_name', $gebruiker['last_name'] );
            update_user_meta( $user->ID, 'company', $gebruiker['bedrijf'] );
            update_user_meta( $user->ID, 'function', $gebruiker['functie'] );
            update_user_meta( $user->ID, 'region', $gebruiker['regio'] );
            update_user_meta( $user->ID, 'market', $gebruiker['product_doelgroep'] );
            update_user_meta( $user->ID, 'phone', $gebruiker['telefoonnummer'] );
            update_user_meta( $user->ID, 'provincie', $gebruiker['provincie'] );
            $wpdb->update($wpdb->users, array(  'user_email'    =>  $gebruiker['email'], 'user_registered'  =>  date("Y-m-d H:i:s")), array('ID' => $user->ID));
        }else{
            $empty_surname_prefix = ($gebruiker['surname_prefix'] == ' ' ? '' : $gebruiker['surname_prefix'].' ');
            $users = wp_insert_user(
                array( // ADD NEW USER TO DATABASE
                    'user_login'    =>  $gebruiker['username'],
                    'user_pass'     =>  $gebruiker['password'],
                    'first_name'    =>  $gebruiker['first_name'],
                    'last_name'     =>  $empty_surname_prefix . $gebruiker['last_name'],
                    'user_email'    =>  $gebruiker['email'],
                    'display_name'  =>  $gebruiker['first_name'] . ' ' . $gebruiker['last_name'],
                    'nickname'      =>  $gebruiker['first_name'] . '' . $gebruiker['last_name'],
                    'role'          =>  'subscriber'
                )                       
            );
            foreach ($data as $update_user) {
                    // ADD ADDITIONAL DATA TO JUST CREATED USER
                    update_user_meta( $users, 'company', $gebruiker['bedrijf'] );
                    update_user_meta( $users, 'function', $gebruiker['functie'] );
                    update_user_meta( $users, 'region', $gebruiker['regio'] );
                    update_user_meta( $users, 'market', $gebruiker['product_doelgroep'] );
                    update_user_meta( $users, 'phone', $gebruiker['telefoonnummer'] );
                    update_user_meta( $users, 'provincie', $gebruiker['provincie'] );
            }
        }if(username_exists( $username ) && strtolower($gebruiker['status']) == 'archived'){// DELETE USER IF STATUS IS ARCHIVED
            require_once(ABSPATH.'wp-admin/includes/user.php' );
            $user = get_user_by( 'login', $username);
            echo $user->ID.'<br>';
            wp_delete_user( $user->ID );
        }
    }
?>

Keep in mind that I am currently working on the error handling so there is no handling now.

Thanks

A generic approach:

//create an array to hold errors:

$errors = [];

//when en error occurs, add it to the array
if($somethingWentWrong){
    $errors[] = 'The error message goes here';
}

//when finished, check for errors and email if found

if(count($errors) > 0){
    mail('hello@somedomain.com', 'some errors happened', implode($erros, "
"));
}