Advertisement
Guest User

Untitled

a guest
Jun 21st, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.11 KB | None | 0 0
  1. <?php
  2. $app->post('/stripe/parse/:total/:offset/:limit/:file(/)', function ($total, $offset, $limit, $file) use ( $app, $reporting, $database ) {
  3.  
  4.     $res = array(
  5.         'code' => 403,
  6.         'message' => 'Invalid request.',
  7.         'url' => null,
  8.         'results' => array()
  9.     );
  10.  
  11.     try {
  12.        
  13.         // setup the header fields
  14.         $head = new parseCSV( 'csv/' . $file, 0, 1 );
  15.         $header_fields = array_keys($head->data[0]);
  16.        
  17.  
  18.         // get the actual data from CSV to insert to db
  19.         $csv = new parseCSV();
  20.         $csv->offset = $offset;
  21.         $csv->limit = $limit;
  22.         $csv->fields = $header_fields;
  23.         $csv->parse( 'csv/' . $file );
  24.  
  25.         $results = array();
  26.         // $insert = array();
  27.  
  28.         $errors = array(); // stores email alerts for failed imports
  29.         $skipped = array();
  30.         $failed = array(); // stores failed rows
  31.  
  32.         foreach ($csv->data as $row) {
  33.             // DO YOUR DB INSERT HERE. $row is your current data
  34.         }
  35.  
  36.         $res['offset'] = $offset;       // the skipped items
  37.         $res['limit'] = $limit;         // number of rows to process in a given iteration
  38.         $res['count'] = $total;         // total # of rows to import
  39.         $res['file_name'] = $file;      // the CSV file
  40.         $res['code'] = 200;             // result code.. alter this when necessary..maybe ensure to get the result of database insert
  41.         $res['message'] = 'Insert successful.';
  42.  
  43.         if( $offset <= $total ){
  44.             $res['next'] = ($offset + $limit) * 1; // the next row to process in the next ajax call
  45.             $res['code'] = 201;         // to signify there's more to import
  46.         }
  47.  
  48.     }
  49.     catch (Exception $e) {
  50.         $res['code'] = 500;
  51.         $res['message'] = $e->getMessage() . ' on file ' . $e->getFile() . ' on line ' . $e->getLine();
  52.     }
  53.  
  54.     $app->response()->header('Content-Type', 'application/json');
  55.    
  56.     if( isset( $_GET['callback'] ) ) {
  57.         echo $_GET['callback'] . '(' . json_encode( $res ) . ')';
  58.     }
  59.     else{
  60.       echo json_encode( $res );
  61.     }
  62. });
  63. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement