不能强制下载 - PHP curl

时间:2012-12-12 01:22:39

标签: php curl fopen

我们将一些数据导出为csv,并使用jQuery ajax表单提交来触发它。我可以得到它的基础工作,我似乎无法得到的是强制下载文件后写入。 PHP

<?php
session_start();
echo $_SESSION['payerEmail'];
echo $_SESSION['password']; 
$url = 'http://www.ninjatrader-support2.com/sugar/FXCMLicense.php';
$FXCMAction = $_REQUEST["FXCMAction"];
$payerEmail = $_SESSION['payerEmail'];
$password = $_SESSION['password'];
$fields = array(
'FXCMAction'=>urlencode($FXCMAction),
'payerEmail'=>urldecode($payerEmail),
'password'=>urldecode($password)
);
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
$fields_string = rtrim($fields_string,'& ');

$ch = curl_init($url);
session_write_close();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
$directory='/home/ninja002/public_html/FXCMReports/';
$filename = 'XCMLicense.csv';
$handle = fopen($directory.$filename, 'w');
fwrite($handle, $output);
echo $output;
curl_close($ch);

?>

与往常一样,任何建议都非常感谢。 TY。

1 个答案:

答案 0 :(得分:0)

希望这可以拯救某些人给我带来的麻烦。这是怎么做的(至少在我的情况下):

<?php
    session_start();
    $url = 'http://www.remotesite/script.php';
    $Foo = $_REQUEST["Foo"];
    $Bar = $_SESSION['Bar'];
    $password = $_SESSION['password'];
    $fields = array(
        'Foo'=>urlencode($Foo),
        'Bar'=>urldecode($Bar),
        'password'=>urldecode($password)
    );
    foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
    $fields_string = rtrim($fields_string,'& ');
    $ch = curl_init($url);
    session_write_close();
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $output = curl_exec($ch);
    curl_close($ch);
    $directory='/home/usr/public_html/Directory/';
    $filename = 'AwesomeData.csv';
    $handle = fopen($directory.$filename, 'w');
    fwrite($handle, $output);
    header('Content-type: application/octet-stream');
    header('Content-Disposition: attachment; filename="AwesomeData.csv"');
    echo $output;
?>