使用cURL从url保存动态excel文件


Use cURL to save dynamic excel file from url

我有一个安全的网站,我使用cURL登录。这很好。然后,我用post数据调用第二个url,它将创建一个返回到浏览器的excel文件。创建文件并将其发送回浏览器需要一两秒钟的时间。我已经尝试了cURL设置,但不能让它将文件保存到我的服务器。下面是我正在尝试的脚本:

    //curl login url
    $login_url = 'https://url.com/control_panel.php';
    //init curl
    $ch = curl_init();
    //Set the URL to work with
    curl_setopt($ch, CURLOPT_URL, $login_url);
    // ENABLE HTTP POST
    curl_setopt($ch, CURLOPT_POST, 1);
    //Set the post parameters
    curl_setopt($ch, CURLOPT_POSTFIELDS, 'username='.$username.'&password='.$password);
    //Handle cookies for the login
    curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
    //return results not true/false
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    //execute the request (the login)
    $store = curl_exec($ch);
    //the login is now done and you can continue to get the protected content.
    //set the URL to the protected file
    curl_setopt($ch, CURLOPT_URL, 'https://url.com/buildexcel');
    //form variables we want
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); 
    $post_data = array(
        'from_month' => date('n', $start_date),
        'from_day' => date('j', $start_date),
        'from_year' => date('Y', $start_date),
        'to_month' => date('n', $end_date),
        'to_day' => date('j', $end_date),
        'to_year' => date('Y', $end_date),
        'all_leagues' => 1,
        'practices' => 1,
        'export' => 'Export Schedule'
    );
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data); 
    //execute the request
    $content = curl_exec($ch);
    curl_close($ch);
    //save the data to disk
    $file = './uploads/excel_'.time().'.csv';
    file_put_contents($file, $content);

任何帮助都太好了。我也试过其他方法,但都没有成功:

    $fp = fopen ($local_file, 'w+');
    //set the URL to the protected file
    curl_setopt($ch, CURLOPT_URL, 'https://url.com/buildexcel');
    //form variables we want
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_TIMEOUT, 50);
    curl_setopt($ch, CURLOPT_FILE, $fp);       
    curl_setopt($ch, CURLOPT_ENCODING, ""); 
    $post_data = array(
        'from_month' => date('n', $start_date),
        'from_day' => date('j', $start_date),
        'from_year' => date('Y', $start_date),
        'to_month' => date('n', $end_date),
        'to_day' => date('j', $end_date),
        'to_year' => date('Y', $end_date),
        'all_leagues' => 1,
        'practices' => 1,
        'export' => 'Export Schedule'
    );
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data); 
    //execute the request
    $content = curl_exec($ch);
    curl_close($ch);
    fclose($fp);

在这段代码之后,它保存了文件,但它是来自页面的html,而不是发送到浏览器的excel。

尝试这个>>>这个代码从url获取数据,但与get Http请求,我认为你只需要(===重要的部分===)

$ch = curl_init();
$csrf_token = $this->getCSRFToken($ch);// this function to get csrf token from website if you need it
$ch = $this->signIn($ch, $csrf_token);//signin function you must do it and return channel
curl_setopt($ch, CURLOPT_HTTPGET, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 300);// if file large
curl_setopt($ch, CURLOPT_URL, "https://your-URL/anything");
$return=curl_exec($ch);
//=== the important part === to save file
$destination ="files.xlsx";
if (file_exists( $destination)) {
    unlink( $destination);
}
$file=fopen($destination,"w+");
fputs($file,$return);
if(fclose($file))
{
    echo "downloaded";
}
curl_close($ch);