duancheng3042 2016-09-18 01:15
浏览 115

如何使用cURL从PHP下载.xls头文件PHP文件

So, I have a PHP script (script 1) that is a basic web page that I echo some text to the user. Whenever the user loads script 1, I want to POST to a PHP script that is has these headers :

header("Content-Type:   application/vnd.ms-excel; charset=utf-8");
header("Content-Disposition: attachment; filename=EXCBOSSTesting.xls"); 
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false);

I am using cURL to call the script but it isn't downloading the file like I want it to. Keep in mind the .xls file is generated once it is called, it doesn't exist as a static file on the server. Here is my code at the moment.

$url = "http://mywebsite.com/script2.php";

$fields = array(
        'foo' => 'bar',
        'user' => 'currUser'
);

$postVars = http_build_query($fields);

//open the connection
$ch = curl_init();

$user = 'example';
$pass = 'examplePass';

$headers = array("Content-Type:   application/vnd.ms-excel; charset=utf-8");

curl_setopt($ch, CURLOPT_USERPWD, $user . ":" . $pass);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POST, count($fields));
curl_setopt($ch, CURLOPT_POSTFIELDS, $postvars);

curl_exec($ch);

// close connection
curl_close($ch);

If I go the script2's URL in my browser, it will download the .xls file and it works just fine. What am I missing here?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 关于#网络安全#的问题:求ensp的网络安全,不要步骤要完成版文件
    • ¥15 可否在不同线程中调用封装数据库操作的类
    • ¥20 使用Photon PUN2解决游戏得分同步的问题
    • ¥15 微带串馈天线阵列每个阵元宽度计算
    • ¥15 keil的map文件中Image component sizes各项意思
    • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
    • ¥15 划分vlan后,链路不通了?
    • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
    • ¥15 Vue3 大型图片数据拖动排序
    • ¥15 Centos / PETGEM