Capture data from CSV file contain variable for API request to save in new CSV file

霸气de小男生 提交于 2020-05-16 22:32:06

问题


I need the tracking number and order number when every line of Unique ID send the API request to the URL, and capture it to save in the new CSV file altogether with the API response.I really need help for this :/

packet.csv (CSV file contains variable for API request)

- Tracking #,Order #,Unique ID
- AB74832493,0dajKDhsa,478324
- CD78437294,kDHIdsan98,768542

track.csv (API response)

delivered,"2020-04-21 13:10:12"
delivered,"2020-02-29 12:55:51"
delivered,"2020-04-21 12:42:16"

desired track.csv (API response) :

Tracking #,Order#,Status,Date  ((get the tracking and order from packet.csv)
0CS7lPuyKzO6,YT2010421266182766,delivered,"2020-04-21 13:10:12"
327231739327,YT7328729173217832,delivered,"2020-02-29 12:55:51"
743287493274,YT7438749327489324,delivered,"2020-04-21 12:42:16"

Controller :

public function getstatusbyid()
    {

        $csv_file = file('C:\wamp64\www\tetsing\application\csv\packet.csv');
        $csv_data = [];
        foreach ($csv_file as $line) {
            $csv_data[] = str_getcsv($line);
        }

        $order_no = json_encode(array_column($csv_data, '0'));
        $tracking = json_encode(array_column($csv_data, '1'));
        $unique_id = array_column($csv_data, '2');
        $access_key = 'SOMETHING';

        if (FALSE === ($fp = fopen('C:\wamp64\www\testing\application\csv\track.csv', 'w'))) {
            die("Error opening CSV file."); // TO DO: handle this better
        }

        foreach ($unique_id as $i => $id) {
            $url = "https://track.my/api/getstatusbyid/$id";

            $data = array(
                'unique_id'       => $id,
                'access_key'      => $access_key
            );

            $data_string = json_encode($data);

            $curl = curl_init();
            curl_setopt($curl, CURLOPT_URL, $url);
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($curl, CURLOPT_POSTFIELDS, $data_string);
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($curl, CURLOPT_TIMEOUT, 120);

            $result = curl_exec($curl);

            curl_close($curl);

            $resultinfo = json_decode($result, true);
            echo '<pre>';
            print_r($resultinfo);

            $status = $resultinfo["status"];
            $date = $resultinfo["last_action_date_time"];

            $resultdata = array();

            $resultdata[] = array(
                'status'    => $status,
                'date'      => $date
            );

            if ($status == 'delivered') {
                foreach ($resultdata as $fields) {
                    fputcsv($fp, $fields);
                }
            }
        }
        fclose($fp);
    }

回答1:


See this updated code, with comments to describe what's happening. Basically, you need to loop through each line in packet.csv, make the appropriate cURL call, and output the results:

public function getstatusbyid()
    {
        $access_key = 'SOMETHING';
        $url = "https://track.my/api/getstatusbyid/$id";

        if (FALSE === ($fp = fopen('C:\wamp64\www\testing\application\csv\track.csv', 'w'))) {
            die("Error opening CSV file."); // TO DO: handle this better
        }

        $csv_file = file('C:\wamp64\www\tetsing\application\csv\packet.csv');

        // Loop through each line in packet.csv:
        foreach ($csv_file as $line) {
            $csv_data = str_getcsv($line); // Get the next line

            list( $order_no, $tracking, $id ) = $csv_data; // Split it up

            $data = array(
                'unique_id'       => $id,
                'access_key'      => $access_key
            );

            $data_string = json_encode($data);

            $curl = curl_init();
            curl_setopt($curl, CURLOPT_URL, $url);
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($curl, CURLOPT_POSTFIELDS, $data_string);
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($curl, CURLOPT_TIMEOUT, 120);

            $result = curl_exec($curl);

            curl_close($curl);

            $resultinfo = json_decode($result, true);
            echo '<pre>';
            print_r($resultinfo);

            $status = $resultinfo["status"];
            $date = $resultinfo["last_action_date_time"];

            // Create the array of data to output:
            $resultdata = array(
                'tracking'  => $tracking,
                'order'     => $order_no,
                'status'    => $status,
                'date'      => $date
            );

            if ($status == 'delivered') {
                fputcsv($fp, $resultdata);
            }

        }

        fclose($fp);
    }


来源:https://stackoverflow.com/questions/61673096/capture-data-from-csv-file-contain-variable-for-api-request-to-save-in-new-csv-f

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!