Using my current code I need to make a separate curl request for on each row of the smartsheet to get the attachment id, then send another request with that id to get the download url. This will occur for each row of the smartsheet and is very inefficient for sheets with lots of rows. Is there a way to get all the attachment id's / url's from a sheet with one request?
class Sheet_request{
private $urlMain = "https://api.smartsheet.com/2.0/users/me";
private $url_food_sheet = "https://api.smartsheet.com/2.0/sheets/3650210866XXXX/?include=attachments";
private $url_food_main= "https://api.smartsheet.com/2.0/sheets/36502108669XXX";
private function curl($url) {
$ch = curl_init($url);
$request_headers = array();
$request_headers[] = "Authorization: Bearer XXXXXXXXXXXXXXXX";
$request_headers[] = "Content-Type: application/json";
curl_setopt($ch, CURLOPT_HTTPHEADER, $request_headers);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
if ($response === false) {
die(curl_error($ch));
}
curl_close($ch);
return $response;
}
function get_attachments() {
$response = $this -> curl($this -> url_food_sheet);
$sheetObj = json_decode($response);
foreach ($sheetObj->rows as $row) {
if (isset($row->attachments)){
$rowAttachmentsURL = $this -> url_food_main . "/rows/" . number_format($row -> id, 0, "", "") . "/attachments";
$getAttachmentsResponse = $this->curl($rowAttachmentsURL);
$attachment = json_decode($getAttachmentsResponse);
$attachmentURL = $this->url_food_main . "/attachments/".number_format($attachment->data[0]->id, 0, "", "");
$attachmentInfo = $this->curl($attachmentURL);
$attachmentInfo = json_decode($attachmentInfo);
file_put_contents("pictures/".$attachmentInfo->name, file_get_contents($attachmentInfo->url));
}
}
}
}
$foo = new Sheet_request();
$foo->get_attachments();