如何使用CURL命令发出PHP请求,将表数据输出到.csv文件,然后在文件准备好后下载该文件?到目前为止,我有:
PHP
if(isset($_GET['FROMCURL'])){
if(isset($_GET['table'])){
$table = $_GET['table'];
}
if($_GET['FROMCURL'] == "test"){
dump_data($db, $table);
}
}
function dumpData($db, $table){
$tables = array("x", "y", "z");
if (in_array($table, $tables)){
try{
$dumpData = $db->prepare("SELECT * FROM $table");
$dumpData->execute();
$data = fopen('../$table_'.time().'.csv', 'a');
while ($row = $dumpData->fetch(PDO::FETCH_ASSOC))
{
$csv = array();
foreach ($row as $key => $val){
if($key != 'x_blob' || $key != 'y_blob' || $key != 'z_blob'){
$csv[] = $val.',';
}
}
fputcsv($data, $csv);
}
}catch(PDOException $e){
echo $e->getMessage();
}
}
}
CURL命令我想有点像
curl http://www.website.com/php/tables.php?FROMCURL=test&table=x
我只是不知道如何让它现在下载文件
使用curl通过get方法获取内容
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"http://www.website.com/php/tables.php?FROMCURL=test&table=x");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
echo curl_exec($ch); // return response
curl_close($ch);
您应该设置标题并echo或打印内容,如下所示:
header('Content-type: text/csv');
header('Content-Disposition: attachment; filename="sqldump.csv"');
echo $csvdata;
你可以下载一个cURL如下的文件:
curl -o sqldump.csv "http://url/here"
其中"-o"表示"输出"