-->

Docusign REST API: Downloading document to string

2019-03-02 14:10发布

问题:

I am building an app using the docusign API and PHP. I have most of this working except I cannot figure out how to download the document. I have been searching on this site and on the Docusign site. Docusign has an example here, that shows how to get a list of docs in PHP, but the downloading does not have a PHP example. In the Docusign REST API docs they explain the method here. But this says the response is "PDF File".

In my sample code below, I have tried to put the contents into a file, but it creates and empty file. If I print_r($data), I get this:

      SplFileObject Object
(
    [pathName:SplFileInfo:private] => /tmp/419ULk
    [fileName:SplFileInfo:private] => 419ULk
    [openMode:SplFileObject:private] => w
    [delimiter:SplFileObject:private] => ,
    [enclosure:SplFileObject:private] => "
)

It does create the file in /tmp, but I want to keep the document in a string so I send or save to DB.

Here is my controller function:

public function get_document($envelopeId, $cert = FALSE)
{
    $save_dir = BASEPATH."../documents/";
    if ($envelopeId) {
        $this->load->model('docusign_model');
        $data = $this->docusign_model->get_document($envelopeId, $cert);
    }
    file_put_contents($save_dir.$envelopeId.".pdf", $data);
    //print_r($data);
    die("116");
}

This is in docusign_model:

public function get_document($envelopeId, $cert)
    {
        $docuSignAuth = $this->auth();
        if ($docuSignAuth) {
            $envelopeApi = new EnvelopesApi($docuSignAuth->apiClient);
            $options = new GetDocumentOptions();
            if($cert) {
                $options->setCertificate(TRUE);
            } else {
                $options->setCertificate(FALSE);
            }
            return $envelopeApi->getDocument($docuSignAuth->accountId, 1, $envelopeId, $options);
        }
        return false;
    }

How can I get this document and keep it in a string?

Any and all help is greatly appreciated!

回答1:

The content comes back as a file, you have to read the temp file and save that to the desired file

Quick snippet using file_get_contents and file_put_contents

$docStream = $envelopeApi->getDocument($accountId, 1, $envelopeId);
file_put_contents("my_document.pdf", file_get_contents($docStream->getPathname()));

More info DocuSign REST API :: EnvelopeDocuments: get under Get a Single Document as a PDF File



标签: docusignapi