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 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line