使用我当前的代码,我需要对智能表的每一行发出单独的 curl 请求以获取附件 id,然后使用该 id 发送另一个请求以获取下载 url。这将发生在 smartsheet 的每一行,并且对于有很多行的工作表来说效率非常低。有没有办法通过一个请求从一张表中获取所有附件 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();