<?
/************************************************************************************
A free to use php class to interact with postalmethods.co
postalmethods.com is an effortless way to send postal mail, using the web
Please contribute to improve this class and send me a copy with your additions at
hide@address.com
***********************************************************************************/
class postal_methods {
var $cusername;
var $cpassword;
var $cdescription;
var $cmode;
function __construct(){
$this->cusername = 'shahzad';
$this->cpassword = '******';
$this->cdescription = 'Sending a letter using Postal Method by Shahzad Jameel';
$this->cmode = 'Development';
}
function SendLetterAndAddressV2(){
$filename = 'PostalMethods_Invoice_Sample5.pdf'; // file to be posted; contents need to conform to requirements (address in proper address area)
//$username = $this->cusername;
//$password = $this->cpassword;
//$description = $this->cdescription;
//$mode = $this->cmode; // Default, Production, or Development.
/************* Settings End ******************/
// Open File
if( !($fp = fopen($filename, "r"))){
// Error opening file
// Handle error however appropriate for your script
echo "Error opening file";
exit;
}
// Read data from the file into $data
$data = "";
while (!feof($fp)) $data .= fread($fp,1024);
fclose($fp);
$soapclient = new SoapClient('https://api.postalmethods.com/PostalWS.asmx?WSDL');
$result = $soapclient->SendLetterAndAddress(array(Username => "shahzad",
Password => "******",
MyDescription => "Sending a letter with Address Inside",
FileExtension => end(explode(".", $filename)),
FileBinaryData => $data, /* this letter contains an empty destination block on the first page */
WorkMode => "Development", /* defines the user's Work Mode setting */
AttentionLine1 => "******************",
AttentionLine2 => "Information Technology",
AttentionLine3 => "",
Company => "***************",
Address1 => "***************",
Address2 => "",
City => "***************",
State => "***********",
PostalCode => "*****",
Country => "USA"));
//print_r($result);die;
$status_code = $result->SendLetterAndAddressResult; // $status_code is the (positive) transaction ID if successful, or a (negative) error number if unsuccessful
//print($status_code);
if ($status_code > 0){
print "Message submitted successfully with transaction ID <b>$status_code</b>";
} else {
print "Message submission failed on error <a href=\"http://www.postalmethods.com/resources/reference/status-codes\">$status_code</a>";
}
} // END FUNCTION SendLetterAndAddress()
function SendLetter(){
/************* Settings Begin ****************/
$filename = 'PostalMethods_Invoice_Sample5.pdf'; // file to be posted; contents need to conform to requirements (address in proper address area)
$username = $this->cusername;
$password = $this->cpassword;
$description = $this->cdescription;
$mode = $this->cmode; // Default, Production, or Development.
/************* Settings End ******************/
// Open File
if( !($fp = fopen($filename, "r"))){
// Error opening file
// Handle error however appropriate for your script
echo "Error opening file";
exit;
}
// Read data from the file into $data
$data = "";
while (!feof($fp)) $data .= fread($fp,1024);
fclose($fp);
$soapclient = new SoapClient('https://api.postalmethods.com/2009-02-26/PostalWS.asmx?WSDL');
$result = $soapclient->SendLetter(array('Username' => $username,
'Password' => $password,
'MyDescription' => $description, // free-form description for your records
'FileExtension' => end(explode(".", $filename)), // make sure the extension reflects the file type
'FileBinaryData' => $data, // PHP5 does base64_encoding implicitly
'WorkMode' => $mode));
//print_r($result);
$status_code = $result->SendLetterResult; // $status_code is the (positive) transaction ID if successful, or a (negative) error number if unsuccessful
if ($status_code > 0){
print "Message submitted successfully with transaction ID <b>$status_code</b>";
} else {
print "Message submission failed on error <a href=\"http://www.postalmethods.com/resources/reference/status-codes\">$status_code</a>";
}
} // END FUNCTON SendLetter()
function GetPDF($status_code) {
/************* Settings Begin ****************/
$username = $this->cusername;
$password = $this->cpassword;
$id = $status_code; // 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>";
}
} // END FUNCTION GetPDF()
function GetLetterStatusV2($status_code){
/************* Settings Begin ****************/
$username = $this->cusername;
$password = $this->cpassword;
$id = $status_code; // ID of transaction being queried
/************* Settings End ******************/
$soapclient = new SoapClient('https://api.postalmethods.com/PostalWS.asmx?WSDL');
$result = $soapclient->GetLetterStatusV2(array('Username' => $username,
'Password' => $password,
'ID' => $id));
// print_r($result);
$result_code = $result->GetLetterStatusV2Result->ResultCode;
if ($result_code == -3000) {
$status = $result->GetLetterStatusV2Result->Status;
$description = $result->GetLetterStatusV2Result->Description;
$lastupdated = $result->GetLetterStatusV2Result->LastUpdateTime;
echo "Letter ID: $id <br>";
echo "Status: $status <br>";
echo "Status Description: $description <br>";
echo "Last Updated: $lastupdated <br>";
} else {
// System returns error message
echo "Error: <a href=\"http://www.postalmethods.com/resources/reference/status-codes\">$result_code</a><br>";
}
}// END FUNCTION GetLetterStatusV2()
} // END CLASS postal_methods
?>