如何从smartsheet api获取附件网址

时间:2015-03-02 18:06:44

标签: php smartsheet-api smartsheet-api-1.1

我正在尝试在$getAttachmentURL中获取附件网址,以便可以下载该文件。我收到错误代码1006 - 找不到。

<?php
$baseURL = "https://api.smartsheet.com/1.1";
$sheetsURL = $baseURL."/sheets/";
$getSheetURL = $baseURL."/sheet/3712544801089412";
$rowsURL = $baseURL."/sheet/3712544801089412/rows";
$rowAttachmentsURL = $baseURL."/sheet/3712544801089412/row/{{ROWID}}/attachments";
$getAttachmentURL = $baseURL."/sheet/3712544801089412/attachment/{{ATTACHMENTID}}";

// Insert your Smartsheet API Token here
$accessToken = "xxx";

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

// Connect to Smartsheet API to get Selected Sheet
$curlSession = curl_init($getSheetURL);

curl_setopt($curlSession, CURLOPT_HTTPHEADER, $header);
curl_setopt($curlSession, CURLOPT_RETURNTRANSFER, TRUE);
$getSheetResponseData = curl_exec($curlSession);
$sheetObj = json_decode($getSheetResponseData);

echo "<h1>Sheet name: ".$sheetObj->name."</h1>";
echo "<br>";
//table start
echo "<table>";
echo "<td>"."Attachment"."</td>";
foreach($sheetObj->columns as $column) {
  echo "<td>".$column->title."</td>";
}

echo "<tr>";
foreach($sheetObj->rows as $row) {

  $rowAttachmentsURL = str_replace('3712544801089412', $row->id, $rowAttachmentsURL);

  $chosenRow = $row->id;
  $rowAttachmentsURL = $baseURL."/sheet/3712544801089412/row/$chosenRow/attachments";
  $rowAttachmentsURL = str_replace($chosenRow, $row->id, $rowAttachmentsURL);

  $curlSession = curl_init($rowAttachmentsURL);

  $rowsResponse = curl_exec($curlSession);

  // Assign response to variable 
  $addRowsObj = json_decode($rowsResponse);

  curl_setopt($curlSession, CURLOPT_HTTPHEADER, $header);
  curl_setopt($curlSession, CURLOPT_RETURNTRANSFER, TRUE);
  $getAttachmentsResponse = curl_exec($curlSession);
  // Assign response to variable
  $attachment = json_decode($getAttachmentsResponse);

  $rowAttachTags = array($chosenRow->id, $theSheet->id, $attachments[0]->id);
  $rowAttachVals = array($addRowsObj->result[0]->id, $theSheet->id, $attachments[0]->id);
  $getAttachmentURL = str_replace($rowAttachTags, $rowAttachVals, $getAttachmentURL);

  $type = $attachment[0]->mimeType;
  $size = $attachment[0]->sizeInKb;
  $filename = $attachment[0]->name;

  // Print headers
  header("Content-Type: $type");
  header("Content-Length: $size");
  header("Content-Disposition: attachment; filename=$filename");

  echo "<td><a href='".$getAttachmentURL."'>".$attachment[0]->name."</a></td>";
  //echo "<td>".$attachment[0]->id."</td>";

  foreach($row->cells as $cell) {
    echo "<td>".$cell->value."</td>";
  }

  echo "<td></td><td></td><td></td>";
  echo "</tr>";
}
echo "</table>";

我有点困惑中止这一切所以我真的需要你的帮助

1 个答案:

答案 0 :(得分:-1)

行附件网址应为行而不是行

此致:

$rowAttachmentsURL = $baseURL."/sheet/3712544801089412/row/{{ROWID}}/attachments";

正确:

$rowAttachmentsURL = $baseURL."/sheet/3712544801089412/rows/{{ROWID}}/attachments";