Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- $app->post('/stripe/parse/:total/:offset/:limit/:file(/)', function ($total, $offset, $limit, $file) use ( $app, $reporting, $database ) {
- $res = array(
- 'code' => 403,
- 'message' => 'Invalid request.',
- 'url' => null,
- 'results' => array()
- );
- try {
- // setup the header fields
- $head = new parseCSV( 'csv/' . $file, 0, 1 );
- $header_fields = array_keys($head->data[0]);
- // get the actual data from CSV to insert to db
- $csv = new parseCSV();
- $csv->offset = $offset;
- $csv->limit = $limit;
- $csv->fields = $header_fields;
- $csv->parse( 'csv/' . $file );
- $results = array();
- // $insert = array();
- $errors = array(); // stores email alerts for failed imports
- $skipped = array();
- $failed = array(); // stores failed rows
- foreach ($csv->data as $row) {
- // DO YOUR DB INSERT HERE. $row is your current data
- }
- $res['offset'] = $offset; // the skipped items
- $res['limit'] = $limit; // number of rows to process in a given iteration
- $res['count'] = $total; // total # of rows to import
- $res['file_name'] = $file; // the CSV file
- $res['code'] = 200; // result code.. alter this when necessary..maybe ensure to get the result of database insert
- $res['message'] = 'Insert successful.';
- if( $offset <= $total ){
- $res['next'] = ($offset + $limit) * 1; // the next row to process in the next ajax call
- $res['code'] = 201; // to signify there's more to import
- }
- }
- catch (Exception $e) {
- $res['code'] = 500;
- $res['message'] = $e->getMessage() . ' on file ' . $e->getFile() . ' on line ' . $e->getLine();
- }
- $app->response()->header('Content-Type', 'application/json');
- if( isset( $_GET['callback'] ) ) {
- echo $_GET['callback'] . '(' . json_encode( $res ) . ')';
- }
- else{
- echo json_encode( $res );
- }
- });
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement