我需要将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:wamp64wwwtestingapplicationcsvpacket.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
);
}
$fp = fopen('C:wamp64wwwtestingapplicationcsvtrack.csv', 'w');
foreach ($resultdata as $fields) {
fputcsv($fp, $fields);
}
fclose($fp);
}
}
}
问题是您重复地重新打开(并重新创建(CSV文件。您可以将"w"更改为"a"以进行追加,或者更好:将对fopen()
和fclose()
的调用移动到foreach ($unique_id as $i => $id) {
之外,这样就不会每次都重复重新打开文件。
在进行此操作时,您还应该检查fopen()
是否失败。
public function getstatusbyid()
{
$csv_file = file('C:wamp64wwwtestingapplicationcsvpacket.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:wamp64wwwtestingapplicationcsvtrack.csv', 'w') ) ) {
die( "Error opening CSV file." ); // TO DO: handle this better
}
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
);
}
foreach ($resultdata as $fields) {
fputcsv($fp, $fields);
}
}
}
fclose($fp);
}