dragon5006 2014-07-22 10:02
浏览 43
已采纳

将智能表保存为服务器上的.xls(curl / php)

i want to save a smartsheet to my server (as .xls). But i always get an .xls filled with json-code. I get the "file_put error" if i use json_decode(..) and the .xls is completely empty. If i do it via curl on my desktop i get the right .xls filled with everything i need.

$baseURL = "https://api.smartsheet.com/1.1";
$headers = array("Authorization: Bearer ".$inputToken);
.
.
array_push($headers,'"Accept: application/vnd.ms-excel" -o  tmpfile.xls --insecure');
$curlSession = curl_init($sheetDetail_url);
curl_setopt($curlSession, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curlSession, CURLOPT_RETURNTRANSFER, TRUE);
$smartsheetData = curl_exec($curlSession);

if (curl_errno($curlSession))
{
    echo "Oh No! Error: " . curl_error($curlSession);
}else{
    // Assign response to PHP object
    $sheetsObj = json_decode($smartsheetData);
    // close curlSession
    curl_close($curlSession);
}
$file1="tmpfile.xls";
if(!(file_put_contents($file1, $sheetsObj))){
    echo "file_put error";
} 

I hope you can help me. Thanks

  • 写回答

1条回答 默认 最新

  • douliaodun9153 2014-07-22 16:25
    关注

    The items that need adjusted in your example are the headers and how the response is handled.

    First, using curl command line options in the headers will not work. Instead you just need to specify that an XLS file should be returned with headers like the following:

    $headers = array("Authorization: Bearer ".$inputToken,
        "Accept: application/vnd.ms-excel");
    

    Second, since an XLS file is going to be returned we will not want to parse that response as JSON. Instead, immediately write the response out to a file.

    With that in mind the following example should work for you and retrieved the specified sheet as an XLS file. Make sure to replace YOUR_TOKEN and YOUR_SHEET_ID with the appropriate values.

    <?php
    $inputToken = 'YOUR_TOKEN';
    $baseURL = "https://api.smartsheet.com/1.1";
    
    $sheetDetail_url = $baseURL.'/sheet/YOUR_SHEET_ID';
    
    $headers = array("Authorization: Bearer ".$inputToken,
        "Accept: application/vnd.ms-excel");
    
    $curlSession = curl_init($sheetDetail_url);
    curl_setopt($curlSession, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($curlSession, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($curlSession, CURLOPT_RETURNTRANSFER, TRUE);
    
    $smartsheetData = curl_exec($curlSession);
    
    // Check for error or save the file
    if (curl_errno($curlSession))
    {
        echo "Oh No! Error: " . curl_error($curlSession);
    }else{
        curl_close($curlSession);
    
        $file1="tmpfile.xls";
        if(!(file_put_contents($file1, $smartsheetData))){
            echo "file_put error";
        }
    }
    
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 matlab有关常微分方程的问题求解决
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法