2015-03-02 69 views
0

我試圖獲取$getAttachmentURL中的附件網址,因此可以下載該文件。我得到錯誤代碼1006 - 找不到。如何從smartsheet api獲取附件網址

<?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

確保$ base網址和{{ATTACHMENTID}}是正確的。嘗試創建自己的手動$ getAttachmentURL路徑並將其粘貼到網址上,以查看是否可以在沒有1006錯誤的情況下訪問該文件。你有沒有在腳本中驗證自己? – unixmiah 2015-03-02 18:23:27

+0

baseURL是正確的,我不能得到附件的網址,我不知道在哪裏可以找到它在smartsheet – user2979664 2015-03-02 20:07:59

+1

@ user2979664你提到$ baseURL是正確的。如何{{ATTACHMENTID}}?你可以從你的curl調用中獲得附件ID嗎?在哪裏使用$ rowAttachmentsURL? – stmcallister 2015-03-02 20:34:20

回答

-1

行附件的URL應該是排不排

此致:

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

正確:

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