将 API 响应导出为 CSV

我需要将我的 API 响应导出到 CSV 文件中。现在,我可以导出它并且输出似乎循环(在我的 API 响应中有 8 个状态交付的项目)。当我检查我的 CSV 文件时,包含 5 行具有相似的输出,其中只有最后一个传递状态的数据。我的错误在哪里,有人可以帮忙吗?:/


API 请求的 CSV:


 - Tracking #,Order #,Unique ID

 - AB74832493,0dajKDhsa,478324

 - CD78437294,kDHIdsan98,768542

API响应:


Array

(

    [status] => delivered

    [last_trace] => 2020-04-21 13:10:00 : Shipment Delivered

    [last_action_date_time] => 2020-04-21 13:10:12

)


Array

(

    [status] => delivered

    [last_trace] => 2020-02-29 12:55:00 : Shipment Delivered

    [last_action_date_time] => 2020-02-29 12:55:51

)

控制器:


public function getstatusbyid()

    {


    $csv_file = file('C:\wamp64\www\testing\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';


    foreach ($unique_id as $i => $id) {

        $url = "https://track.my/api/getstatus/$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, 15);


        $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();

        if ($status == 'delivered') {

            foreach ($resultinfo as $item) {


                $resultdata[] = array(

                    'status'    => $status,

                    'date'      => $date

                );

            }

一只萌萌小番薯
浏览 113回答 1
1回答

qq_花开花谢_0

问题是您反复重新打开(并重新创建)CSV 文件。您可以将 'w' 更改为 'a' 以附加,或者甚至更好:将调用移至fopen()外部fclose(),foreach ($unique_id as $i => $id) {这样您就不会每次都重复重新打开文件。当你这样做的时候,你还应该检查fopen()失败。public function getstatusbyid()&nbsp; &nbsp; {&nbsp; &nbsp; $csv_file = file('C:\wamp64\www\testing\application\csv\packet.csv');&nbsp; &nbsp; $csv_data = [];&nbsp; &nbsp; foreach ($csv_file as $line) {&nbsp; &nbsp; &nbsp; &nbsp; $csv_data[] = str_getcsv($line);&nbsp; &nbsp; }&nbsp; &nbsp; $order_no = json_encode(array_column($csv_data, '0'));&nbsp; &nbsp; $tracking = json_encode(array_column($csv_data, '1'));&nbsp; &nbsp; $unique_id = array_column($csv_data, '2');&nbsp; &nbsp; $access_key = 'SOMETHING';&nbsp; &nbsp; if ( FALSE === ( $fp = fopen('C:\wamp64\www\testing\application\csv\track.csv', 'w') ) ) {&nbsp; &nbsp; &nbsp; &nbsp; die( "Error opening CSV file." ); // TO DO: handle this better&nbsp; &nbsp; }&nbsp; &nbsp; foreach ($unique_id as $i => $id) {&nbsp; &nbsp; &nbsp; &nbsp; $url = "https://track.my/api/getstatus/$id";&nbsp; &nbsp; &nbsp; &nbsp; $data = array(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'unique_id'&nbsp; &nbsp; &nbsp; &nbsp;=> $id,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'access_key'&nbsp; &nbsp; &nbsp; => $access_key&nbsp; &nbsp; &nbsp; &nbsp; );&nbsp; &nbsp; &nbsp; &nbsp; $data_string = json_encode($data);&nbsp; &nbsp; &nbsp; &nbsp; $curl = curl_init();&nbsp; &nbsp; &nbsp; &nbsp; curl_setopt($curl, CURLOPT_URL, $url);&nbsp; &nbsp; &nbsp; &nbsp; curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);&nbsp; &nbsp; &nbsp; &nbsp; curl_setopt($curl, CURLOPT_POSTFIELDS, $data_string);&nbsp; &nbsp; &nbsp; &nbsp; curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);&nbsp; &nbsp; &nbsp; &nbsp; curl_setopt($curl, CURLOPT_TIMEOUT, 15);&nbsp; &nbsp; &nbsp; &nbsp; $result = curl_exec($curl);&nbsp; &nbsp; &nbsp; &nbsp; curl_close($curl);&nbsp; &nbsp; &nbsp; &nbsp; $resultinfo = json_decode($result, true);&nbsp; &nbsp; &nbsp; &nbsp; echo '<pre>';&nbsp; &nbsp; &nbsp; &nbsp; print_r($resultinfo);&nbsp; &nbsp; &nbsp; &nbsp; $status = $resultinfo["status"];&nbsp; &nbsp; &nbsp; &nbsp; $date = $resultinfo["last_action_date_time"];&nbsp; &nbsp; &nbsp; &nbsp; $resultdata = array();&nbsp; &nbsp; &nbsp; &nbsp; if ($status == 'delivered') {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach ($resultinfo as $item) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $resultdata[] = array(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'status'&nbsp; &nbsp; => $status,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'date'&nbsp; &nbsp; &nbsp; => $date&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; );&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach ($resultdata as $fields) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fputcsv($fp, $fields);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; fclose($fp);}
打开App,查看更多内容
随时随地看视频慕课网APP