0

I'm trying to download a docusign envelope with curl. I'm accessing the envelope but the pdf won't show. I see a web page with symbols and can't save the pdf of the envelope. Any help appreciated.

require_once('rest/autoload.php');


$url = "https://demo.docusign.net/restapi/v2/login_information";
$header = "<DocuSignCredentials><Username>" . 
$config['username_docusign'] . "</Username><Password>" . 
$config['password_docusign'] . "</Password><IntegratorKey>" . 
$config['api_firmadoc'] . "</IntegratorKey></DocuSignCredentials>";



$curl = curl_init($url);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, array("X-DocuSign-Authentication: 
$header"));

$json_response = curl_exec($curl);
$status = curl_getinfo($curl, CURLINFO_HTTP_CODE);

if ( $status != 200 ) {
echo "error calling webservice, status is:" . $status;
exit(-1);
}


$response = json_decode($json_response, true);
$accountId = $response["loginAccounts"][0]["accountId"];
$baseUrl = $response["loginAccounts"][0]["baseUrl"];
curl_close($curl);



$curl = curl_init("https://demo.docusign.net/restapi/v2/accounts/1319127/envelopes/cc4aa62f-506c-4ee4-b6a3-dc9ae3e5fa14/documents/combined" );


curl_setopt($curl, CURLOPT_HTTPHEADER, array(                                                                          

'Content-Type: application/pdf',
'X-DocuSign-Authentication: { "Username":"****", "Password":"****", 
 "IntegratorKey":"*****" }' )                                                                       
 );
$json_response = curl_exec($curl);
$status = curl_getinfo($curl, CURLINFO_HTTP_CODE);


echo $json_response;

I expected a pdf but i receive a web page with symbols

1 Answers1

0
  1. You're using the obsolete legacy authentication. Much better idea would be to use OAuth.
  2. You're not setting the Content-Type of your php page, instead you're setting the Content-Type of your request to DocuSign. (Which is also wrong, you should be setting the Accept header to application/pdf)
  3. To set the Content-Type of your page, add the following at the top of your php script: header("Content-type:application/pdf"); See this SO answer.

Check out the PHP JWT and Authorization Code Grant examples for more info.

Thank you for integrating with DocuSign!

Larry K
  • 47,808
  • 15
  • 87
  • 140