PHP: Retrieve The PDF Document Of The Letter Or Postcard With GetPDF()

<?php
 
/************* Settings  Begin ****************/
 
$username = '********';
$password = '********';
$id = 99999999; // ID of transaction being queried
$output_filename = 'PostalMethods_' . $id . '.pdf'; // Comment out to view on screen
 
/************* Settings  End ******************/
 
$soapclient = new SoapClient('https://api.postalmethods.com/2009-02-26/PostalWS.asmx?WSDL');
$result = $soapclient->GetPDF(array('Username'    => $username,
                                    'Password'    => $password,
                                    'ID'   => $id));
 
//print_r($result);
//exit;
 
// Extract returned fields
$result_code = $result->GetPDFResult->ResultCode;
$file_data   = $result->GetPDFResult->FileData;
 
if ( $result_code == -3000) { 
    // an letter has been successfully retrieved
    if ($output_filename != ''){ 
        // output to file
		print "PDF successfully retrieved<br>";
        if (!$handle = fopen($output_filename, 'w')) {
            echo "Cannot open file ($output_filename)";
            exit;
            }
        if (fwrite($handle, $file_data) === FALSE) {
            echo "Cannot write to file $output_filename";
            exit;
            }
		print "PDF successfully written<br>";
        fclose($handle);
        } else { 
            // output to screen
            header ("Content-type: application/pdf"); 
            echo $file_data;
            }
 
} else {
    // no letter retrieved
    echo "Error: <a href=\"http://www.postalmethods.com/resources/reference/status-codes\">$result_code</a><br>";
}
 
?>

Continue to: PHP: Accept status callback by HTTP POST from PostalMethods

Or

Go back to: ‹PHP: Send PDF or other binary file with SendLetter()
Send Letters
Securely + Reliably
Send letters securely and reliably from any business application. Use for invoices, quotes, and other important business communications. Enjoy special web-form to postal features and instant notifications.
Simple Process
Easy as 1-2-3
Experience the ease of our streamlined process - developed for efficiency! You submit your document by email or API. We automatically print, collate, insert, and stamp your letter. Your letter is delivered via standard postal service.
Pay As You Go
No Fuss. No Obligations.
Register for free with no obligations! Evaluate the service as long as you need. Once you are ready, set up our user-friendly pay-as-you option to use our service. No fuss. No on-going commitment. Just plain easy!