使用cURL从网址

时间:2016-10-06 12:29:10

标签: php curl

我有一个安全的网站,我使用cURL登录。这很好用。然后,我使用发布数据调用第二个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。

1 个答案:

答案 0 :(得分:0)

尝试此>>>这段代码可从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);