如何通过一个请求获取所有智能表附件

时间:2015-09-17 15:11:04

标签: php smartsheet-api smartsheet-api-2.0

使用我当前的代码,我需要在智能表的每一行上单独生成一个curl请求以获取附件ID,然后发送另一个带有该id的请求以获取下载URL。 对于智能表的每一行都会出现这种情况,对于包含大量行的工作表而言效率非常低。 有没有办法通过一个请求从工作表中获取所有附件ID / url?

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();

2 个答案:

答案 0 :(得分:1)

您可以使用Get All Attachments操作在指定工作表的单个请求中检索所有附件(ID)。如文档所示,请求URI为:https://api.smartsheet.com/2.0/sheets/{sheetId}/attachments

请注意,附件可以存在于Smartsheet中的3个不同级别:工作表,行,注释。 获取所有附件响应将在工作表中包含所有附件(在任何/所有级别:工作表,行,注释) - 因此,如果您只是对行附件感兴趣,您只想处理响应中的项目,其中 parentType 属性的值为" ROW"。

在"获取所有附件"后,您可以访问附件ID。在回复中,您仍然需要随后对每个附件使用Get Attachment操作来一次检索一个URL。 (目前还无法批量获取这些产品。)

答案 1 :(得分:0)

在工作表或行级别,我相信您现在可以添加“ include”查询参数来强制将附件包括在.sheet中。和.row。级别,确保.comment。级别的附件,需要在包含值中添加讨论:

https://api.smartsheet.com/2.0/sheets/{sheetId}?include=attachments,discussions