|
|
|
ถ้าผมต้องการ export data to csv file บน Linux Output ที่ต้องการคือสร้างไฟล์ filename.csv ขึ้นมาใน directory ทำอย่างไรครับ |
|
|
|
|
|
|
|
Code (PHP)
<?php
# Prepare the data to query
$data = array(
'uid' => 'xxx',
'pass' => 'yyy',
'cmd' => 'zzz',
'sDate' => '20180723100000',
'eDate' => '20180723110000'
);
# Create a connection format
$url = 'http://pro.mobileinnovation.asia:81/WFO/getRecord';
$postString = http_build_query($data, '', '&');
// Prepare csv format on the header
header('Content-Type: text/csv; charset=utf-8');
// File name
header('Content-Disposition: attachment; filename=data.csv');
$output = fopen("php://output", "w");
// Prepare the header of CSV file
fputcsv($output,
array(
'Car Number','Class Number','Record Time','Dstb Sum','Lat0','Lan0','Lat10','Lon10','Lat20','Lon20','Lat30','Lon30',
'Lat40','Lon40','Lat50','Lon50','GPS','Direction','Speed','RecReserver1','Speed Avg','Speed Max','Speen Min','Rpm Avg',
'Rpm Max','Rpm Min','Analog1 Avg','Analog1 max','Analog1 min','Analog2 Avg','Analog2 max','Analog2 min','Analog3 Avg',
'Analog3 max','Analog3 min','Analog4 Avg','Analog4 max','Analog4 min','Digi Sw1','Digi Sw2','Digi Sw3','Digi Sw4','Fuel'
));
// Get data by curl function
$ch = curl_init();
//curl_setopt ($ch, CURLOPT_PORT , 81);
curl_setopt($ch, CURLOPT_VERBOSE, true);
//curl_setopt ($ch, CURLOPT_PORT , 81);
curl_setopt($ch, CURLOPT_URL, $url);
//curl_setopt ($ch, CURLOPT_PORT , 81);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postString);
$content = curl_exec ($ch);
$lines = explode(" ", $content);
foreach($lines as $index => $line)
{
$lines[$index] = $line . '<br/>';
}
?>
Tag : PHP, Linux
|
|
|
|
|
|
Date :
2018-07-31 14:54:20 |
By :
top8891 |
View :
830 |
Reply :
4 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Code
$output = fopen("path/to/file", "w");
foreach($lines as $index => $line)
{
fputcsv($output, $line)
}
|
|
|
|
|
Date :
2018-07-31 18:51:51 |
By :
DK |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
http://php.net/manual/en/function.fopen.php
http://php.net/manual/en/function.fputcsv.php
|
|
|
|
|
Date :
2018-07-31 18:53:53 |
By :
DK |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ใช่แบบนี้มั้ยครับ คุณ DK ผม Run แล้วไม่มีไฟล์ออกมา
Code (PHP)
<?php
$url = 'http://pro.mobileinnovation.asia:81/WFO/getRecord';
$data = array(
'uid' => 'zzz',
'pass' => 'yyy',
'cmd' => 'zzz',
'sDate' => '20180723100000',
'eDate' => '20180723110000'
);
$postString = http_build_query($data, '', '&');
header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; filename=data.csv');
$output = fopen("/samba/", "w");
fputcsv($output,
array(
'Car Number','Class Number','Record Time','Dstb Sum','Lat0','Lan0','Lat10','Lon10','Lat20','Lon20','Lat30','Lon30',
'Lat40','Lon40','Lat50','Lon50','GPS','Direction','Speed','RecReserver1','Speed Avg','Speed Max','Speen Min','Rpm Avg',
'Rpm Max','Rpm Min','Analog1 Avg','Analog1 max','Analog1 min','Analog2 Avg','Analog2 max','Analog2 min','Analog3 Avg',
'Analog3 max','Analog3 min','Analog4 Avg','Analog4 max','Analog4 min','Digi Sw1','Digi Sw2','Digi Sw3','Digi Sw4','Fuel'
));
$ch = curl_init();
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postString);
$content = curl_exec ($ch);
$lines = explode(" ", $content);
foreach($lines as $index => $line)
{
fputcsv($output, $line)
}
?>
|
|
|
|
|
Date :
2018-08-01 13:52:06 |
By :
top8891 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$output = fopen("/samba/", "w"); path ถูกหรือเปล่า เอาง่ายๆ ภายใต้ directory ที่คุณรัน เปลี่ยนจาก ไฟล์ xxx.php เป็น /samba แล้วเจอ folder ไหม
|
|
|
|
|
Date :
2018-08-01 16:14:29 |
By :
DK |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 03
|