我需要将我的 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
);
}
qq_花开花谢_0