dourong9253 2017-02-07 23:07
浏览 21
已采纳

如何从PHP API访问Smartsheet?

It is my first time trying to connect Sheet from Smartsheet using API with PHP.
I cannot seem to connect and give me this error

Notice: Trying to get property of non-object in C:\xampp\htdocs\smartsheet\test.php on line 22

The variable $sheetObj is empty.
And in Authorization: Bearer, what does Bearer means? Is it a token name or it is always Bearer?

My future plan is to write into the row of smartsheet using PHP. Can anyone give me advice what went wrong with my code?

$baseURL = "https://api.smartsheet.com/1.1";
$sheetsURL = $baseURL . "/sheets/";
$getSheetURL = $baseURL . "/sheet/xxxxxxxxxxx";
$rowsURL = $baseURL . "/sheet/xxxxxxxxxxx/rows";

$accessToken = "xxxxxxxxxxxxxxxxxx"; 

// Create Headers array for cURL
$headers = array(
    "Authorization: Bearer " . $accessToken,
    "Content-Type: application/json"
);  

$curlSession = curl_init($getSheetURL);
curl_setopt($curlSession, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curlSession, CURLOPT_RETURNTRANSFER, TRUE);
$getSheetResponseData = curl_exec($curlSession);

$sheetObj = json_decode($getSheetResponseData);
echo "<h1>Sheet name: ". $sheetObj->name ."</h1>";
  • 写回答

4条回答 默认 最新

  • dongzaheng4449 2017-02-08 16:35
    关注

    Both stmcallister and Kim provided good information on how to troubleshoot your issue and some likely causes.

    There were actually two issues with the code you provided.

    1. As Scott mentioned you must point to the 2.0 version of the API.

      $baseURL = "https://api.smartsheet.com/2.0";
      
    2. You have a typo in your $getSheetURL. As is documented here the url is /sheets/{sheetId}. So your code should have the following:

      $getSheetURL = $baseURL. "/sheets/xxxxxxxxxxx";
      

    Here is your code in a working state. Make sure to replace YOUR_TOKEN and also take a look at the output from var_dump (which I added to your code) to see what message it gives you.

    <?php
    $baseURL = "https://api.smartsheet.com/2.0";
    $getSheetURL = $baseURL. "/sheets/4925037959505796";
    $accessToken = "YOUR_TOKEN"; 
    
    $headers = array("Authorization: Bearer ". $accessToken);
    
    $curlSession = curl_init($getSheetURL);
    curl_setopt($curlSession, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($curlSession, CURLOPT_RETURNTRANSFER, TRUE);
    $getSheetResponseData = curl_exec($curlSession);
    
    // Remove this line when done debugging
    var_dump($getSheetResponseData);
    
    $sheetObj = json_decode($getSheetResponseData);
    
    echo "<h1>Sheet name: ". $sheetObj->name ."</h1>";
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?