简体   繁体   中英

Smartsheet - How to attach a file to a row using Smartsheet API and PHP curl

The smartsheet api docs provide this documentation to attach a file to a row.

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

\\ -H "Authorization: Bearer ll352u9jujauoqz4gstvsae05" \\ -H "Content-Type: application/msword" \\ -H 'Content-Disposition: attachment; filename="ProgressReport.docx"' \\ -H "Content-Length: FILE_SIZE" \\ -X POST \\ --data-binary @ProgressReport.docx

How do I translate this into the appropriate calls to a curl session in php?

I assume that I need to create a header block such as

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

and use it like so

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

What do I need to put in $body so that the data from the file is uploaded?

The smartsheet api docs say

The following example request shows a simple upload that adds a file attachment to a sheet:

POST https://api.smartsheet.com/2.0/sheets/4509093797881732/attachments Authorization: Bearer ll352u9jujauoqz4gstvsae05 Content-Disposition: attachment; filename="ProgressReport.docx" Content-Type: application/msword Content-Length: 5463

< Binary content for file >

As shown in this example, the contents of the file is included in the body of the POST request. In most programming languages, this is done by reading the file from an input stream and writing it out to the output stream of the HTTP request.

this is done by reading the file from an input stream and writing it out to the output stream of the HTTP request. <--- How Do I Do This In PHP?

As an after thought, how can I check this with Postman?

Thanks in advance. Any help would be awesome.

While Smartsheet does not have a PHP SDK, you are essentially using a secondary language to send REST commands and JSON over the wire.

You'll need the following headers:

 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

I believe you can put the path in your filename param if you need to. And the file size is in bytes.

It would appear that Postman had the answer all the time. Postman has an option to generate code snippets, which up to now I had not read correctly, as I was fixated on CURL. I then noticed on the left hand side there was an option for PHP - cURL which gave me the answer I wanted. Using this code finally worked for me.

<?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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM