Reputation: 7
I am working on Smartsheet API but I am not a pro. I need some help to get all the information out a specific sheet. With all information I mean all the columns and all attachments to them. I also need help to make it possible to download the attachments to the users device. Is there any one out there with a template in PHP? I have come so far to get the information from the columns out but it is not enough.
$baseURL = "https://api.smartsheet.com/1.1";
$sheetsURL = $baseURL. "/sheets/";
$getSheetURL = $baseURL. "/sheet/xxx";
$rowsURL = $baseURL. "/sheet/xxx/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
$headers = 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, $headers);
curl_setopt($curlSession, CURLOPT_RETURNTRANSFER, TRUE);
$getSheetResponseData = curl_exec($curlSession);
$sheetObj = json_decode($getSheetResponseData);
//attachment
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) {
$curlSession = curl_init($rowAttachmentsURL);
curl_setopt($curlSession, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curlSession, CURLOPT_RETURNTRANSFER, TRUE);
$getAttachmentsResponse = curl_exec($curlSession);
// Assign response to variable
$attachments = json_decode($getAttachmentsResponse);
$rowAttachmentsURL = str_replace('{{ROWID}}', $row->id, $rowAttachmentsURL);
var_dump($attachments);
foreach ($row->attachments as $attachment) {
echo "<td>".$attachment->name."</td>";
}
foreach ($row->cells as $cell)
{
echo "<td>".$cell->value."</td>";
}
echo "<td></td><td></td><td></td>";
echo "</tr>";
}
echo "</table>";
curl_close($curlSession);
?>
Upvotes: 1
Views: 1253
Reputation: 1719
There is a project on GitHub that provides php sample code for working with the Smartsheet API. It sounds like you're looking to get all the columns in your sheet, as well as work with attachments.
To see how to get the contents of your sheet, including columns, I would recommend the HelloGetSheet sample. This sample gets a selected sheet and then outputs the columns and rows for that sheet.
Inside that same project, there is also sample code for working with Attachments. In addition to showing how to upload an attachment, this script shows how to retrieve and download attachments from your sheet.
UPDATE: Thanks for posting the code. You look like you have the main building blocks you need to accomplish what you're looking for. I would suggest moving your 'List Row Attachments' section inside your row loop. Then, for reach row you can grab the row ID and use that to get your attachments URL like so
$rowAttachmentsURL = str_replace('{{ROWID}}', $row->id, $rowAttachmentsURL);
Upvotes: 1