0

smartsheet api 文档提供此文档以将文件附加到行。

curl https://api.smartsheet.com/2.0/sheets/{sheetId}/rows/{rowId}/attachments

\ -H "授权:承载ll352u9jujauoqz4gstvsae05" \ -H "内容-类型:应用程序/msword" \ -H '内容-处置:附件;filename="ProgressReport.docx"' \ -H "Content-Length: FILE_SIZE" \ -X POST \ --data-binary @ProgressReport.docx

如何将其转换为对 php 中 curl 会话的适当调用?

我假设我需要创建一个标题块,例如

$headersAttachment = array(
    "Authorization: Bearer " . $AccessToken,
    "Content-Type: application/pdf",
    'Content-Disposition: attachment; filename="mydoc.pdf"', // **is this for just the filename or the full path?**
    "Content-Length: " . $FileSizeOfMyDoc 
);  

并像这样使用它

$curlSession = curl_init($rowsURL . '/' . $rowId . '/attachments');
curl_setopt($curlSession, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($curlSession, CURLOPT_HTTPHEADER, $headersAttachment);
curl_setopt($curlSession, CURLOPT_POSTFIELDS, $body);
curl_setopt($curlSession, CURLOPT_POST, TRUE);
$getSheetResponseData = curl_exec($curlSession);

我需要在 $body 中放入什么才能上传文件中的数据?

smartsheet api 文档说

以下示例请求显示了将文件附件添加到工作表的简单上传:

POST https://api.smartsheet.com/2.0/sheets/4509093797881732/attachments 授权:承载 ll352u9jujauoqz4gstvsae05 内容处置:附件;filename="ProgressReport.docx" 内容类型:应用程序/msword 内容长度:5463

<文件的二进制内容>

如本例所示,文件的内容包含在 POST 请求的正文中。在大多数编程语言中,这是通过从输入流中读取文件并将其写入 HTTP 请求的输出流来完成的。

这是通过从输入流中读取文件并将其写入 HTTP 请求的输出流来完成的。 <--- 我如何在 PHP 中做到这一点?

作为一个事后的想法,我怎么能用邮递员检查这个?

提前致谢。任何帮助都是极好的。

4

2 回答 2

1

虽然 Smartsheet 没有 PHP SDK,但您实际上是在使用辅助语言通过网络发送 REST 命令和 JSON。

您将需要以下标题:

POST /2.0/sheets/{sheetId}/rows/{rowId}/attachments
Host: api.smartsheet.com
Content-Type: image/jpeg; charset=binary
Accept-Encoding: gzip, deflate, br
Content-Disposition: attachment; filename=“Image.jpeg"
Content-Length: 1048945
Authorization: Bearer TOKENHERE

我相信如果需要,您可以将路径放在文件名参数中。文件大小以字节为单位。

于 2020-02-04T20:58:44.420 回答
0

看起来邮递员一直都有答案。Postman 可以选择生成代码片段,到目前为止我还没有正确阅读,因为我一直在关注 CURL。然后我注意到在左侧有一个 PHP 选项 - cURL 给了我想要的答案。使用此代码终于对我有用。

<?php

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => "https://api.smartsheet.com/2.0/sheets/SHEETID/rows/ROWID/columns/COLOUMNID/cellimages",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 0,
  CURLOPT_FOLLOWLOCATION => true,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "POST",
  CURLOPT_POSTFIELDS => "<file contents here>",
  CURLOPT_HTTPHEADER => array(
    "Authorization: Bearer ACCESSTOKEN",
    "Content-Type: image/png",
    "Content-Disposition: attachment; filename=\"modify.png\"",
    "Content-Length: <file size here>
  ),
));

$response = curl_exec($curl);

curl_close($curl);
echo $response;
于 2020-02-16T18:49:06.937 回答