从CSV文件捕获数据包含API请求变量以保存在新的CSV文件中 [英] Capture data from CSV file contain variable for API request to save in new CSV file

查看:69
本文介绍了从CSV文件捕获数据包含API请求变量以保存在新的CSV文件中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每行唯一ID将API请求发送到URL并将其捕获以与API响应一起保存到新的CSV文件中时,我需要跟踪号和订单号.我真的需要帮助:/

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文件包含用于API请求的变量)

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

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

track.csv(API响应)

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"

所需的track.csv(API响应):

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"

控制器:

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);
    }

推荐答案

请参阅此更新的代码,并附有描述事件的注释.基本上,您需要遍历packet.csv中的每一行,进行适当的cURL调用,然后输出结果:

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);
    }

这篇关于从CSV文件捕获数据包含API请求变量以保存在新的CSV文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆