Attach file to row using PHP

Options
Roger Jarrett
Roger Jarrett ✭✭
edited 02/05/20 in API & Developers

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.

Answers

  • Genevieve P.
    Genevieve P. Employee Admin
    Options

    Hi Roger,

    We don't currently support PHP, so I don't have any advice for you in that area. Essentially, you will be passing REST + JSON over the wire. You will need to sort out the following headers for your PHP calls:

    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


    The file size is in bytes. I believe you can put the path in your filename param if you need to. I was talking with a colleague and it sounds like you have already sorted this out with them, so that's great! Let us know if you have any other questions.

    Cheers,

    Genevieve

  • Jaime Rossello
    Options

    @Roger Jarrett Did you figure out if this is possible? how?

    Cheers

  • Roger Jarrett
    Options

    This is how I did it

        $baseURL = "https://api.smartsheet.com/2.0";

    $sheetsURL = $baseURL . "/sheets/";

      $AccessToken = '<<Your Access Token>>' ;

      $SheetId = '<<Your Sheet Id>>' ;

      $sheetURL = $baseURL . "/sheets/" . $SheetId;

      $rowsURL = $sheetURL . "/rows";

    $rowId = '<<Your Row Id>>' ;


    $AttachmentPath = 'File name/path of File to Upload.pdf' ;

    $AttachmentLength = filesize($AttachmentPath) ;

    $PDFContent = file_get_contents($AttachmentPath) ;


    $PDFUrl = $rowsURL . '/' . $rowId . '/attachments' ;

    $headersAttachment = array(

    "Authorization: Bearer " . $AccessToken,

    "Content-Type: application/pdf",

    'Content-Disposition: attachment; filename="' . pathinfo($AttachmentPath, PATHINFO_FILENAME) . '.pdf"',

    "Content-Length: " . $AttachmentLength

    );

    $curlSession = curl_init();

    curl_setopt_array($curlSession, array(

    CURLOPT_URL => $PDFUrl,

    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 => $PDFContent,

    CURLOPT_HTTPHEADER => $headersAttachment,

    CURLOPT_SSL_VERIFYHOST => false,

    CURLOPT_SSL_VERIFYPEER => false,

    )

    );

    $SheetResponseData = curl_exec($curlSession);


    if ($SheetResponseData === FALSE)

    {

    if (curl_errno($curlSession))

    {

    $ErrMsg .= 'Curl error: ' . curl_error($curlSession) . '<br>' ;

    }

    }

    else

    {

    $sheetObj = json_decode($SheetResponseData, true);

    if (strstr($SheetResponseData, 'errorCode') !== FALSE)

    {

    $ErrMsg .= 'Data error: [' . $sheetObj['errorCode'] . '] ' . $sheetObj['message'] . '<br>' ;

    }

    else

    {

    // now extract the rowId of the newly created row

    // $rowId = sprintf("%.0f", $sheetObj['result']['id']) ;

    $Success = TRUE ;

    }

    }