0

我正在尝试获取附件 url,$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>";

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

4

1 回答 1

-1

行附件 url 应该是行而不是行

你的:

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

正确的:

$rowAttachmentsURL = $baseURL."/sheet/3712544801089412/rows/{{ROWID}}/attachments";
于 2018-02-06T23:56:00.737 回答