duanbi3151 2018-04-24 02:49
浏览 118

如果我在服务器上调用api,我应该在代码中更改一些内容吗?

I make a php page to look up the database and make Excel file to download

I make a Excel.php

<?php
...
$filename = "Sample";
            $objPHPExcel->getActiveSheet()->setTitle("Tasks-Overview");

            header('Content-Encoding: UTF-8');
            header('Content-type: application/vnd.ms-excel');
            header('Content-Disposition: attachment; filename="'.$filename.'".xls');
            $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel,'Excel5');
            $objWriter->save('php://output');

            exit;

?>

And this is CallFile.php

<?php 

    $url = 'https://#url';

    $ch = curl_init();
        curl_setopt($ch,CURLOPT_ENCODING, "UTF-8" );
        curl_setopt($ch,CURLOPT_URL,$url);
        curl_setopt($ch,CURLOPT_GET,1);
        curl_setopt($ch,CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch,CURLOPT_HTTPHEADER, array('Content-Type: appliaction/json'));

    $res = curl_exec($ch);
    echo print_r($res,true);
?>

And then I click the button, the excel file will download.

When I put address(Excel.php) in Web address bar, it download Excel file success, but when I call CallFile.php it occurs error like this and didn't download excel file.

...
10-1101000000000 � ��*+������&ffffff�?'ffffff�?(�?)�?�"dXX333333�?333333�?U}}}}}}}}     � � � � � � � � � �   �  � � �  � � @� � � � � � � @� � � � � � � >�@d��d�Bgg����� 
...

Webpage just print error message like break word.

I don't know why I could get Excel file when I call Excel.php,and couldn't get Excel file on CallFile.php

I doubt problem in call api, so I test just echo on Excel.php, and call from CallFile.php, it prints echo without problem.

If you know about that, please help me

  • 写回答

1条回答 默认 最新

  • dsozqcx9668 2018-04-24 02:59
    关注

    You shouldn't use print_r if you're trying to output a binary file. print_r adds extra text around variables to make it more readable for people, but that will also screw with it being an excel file. Use echo instead. Also consider setting content disposition headers like you have in your first example to try and ensure the browser downloads it instead of trying to display it like text.

    评论

报告相同问题?

悬赏问题

  • ¥15 shape_predictor_68_face_landmarks.dat
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制