Send real letters from the Internet to anywhere in the world.
# Wednesday, 21 August 2013

We've had several requests for an example on using our SOAP API, so decided to throw one together for you to get an idea of how it works.

The example below will send PDF documents as a letter with a cover letter which is written as a formatted HTML string (see the LetterBody attribute below).

To make more use of this example and to adapt it to your needs you may need to look at the Letter Pricing API along with the country code list which can all be found here.

If you would like us to advise you on what are the best options to use for postage and paper usage please contact us.

   1:  <?php
   2:   
   3:  $client = new SoapClient("https://www.pc2paper.co.uk/lettercustomerapi.svc?wsdl");
   4:   
   5:  // Your PC2Paper Username and Password go below
   6:  $pc2paperUsername = "username";
   7:  $pc2paperPassword = "password";
   8:   
   9:  //First we upload a PDF we wish to attach to our letter
  10:   
  11:  $file = file_get_contents("C:\tempfiles\mytestfile.pdf");
  12:   
  13:  //We give the pdf a friendly name
  14:  $parametersUpload->filename = "mytestFile.pdf";
  15:  $parametersUpload->fileContent = $file;
  16:  $parametersUpload->username=$pc2paperUsername;
  17:  $parametersUpload->password=$pc2paperPassword;
  18:   
  19:  //We submit our PDF to PC2Paper
  20:  $uploadedFileResult = $client->UploadDocument($parametersUpload);
  21:   
  22:  // Not the above return value contains the following
  23:  // $uploadedFileResult->UploadDocumentResult->ErrorMessages  (A a string array of any errors that took place)
  24:  // $uploadedFileResult->UploadDocumentResult->FileCreatedGUID (The created files GUID)0
  25:  // $uploadedFileResult->UploadDocumentResult->Status (OK or ERROR, if error check ErrorMessages array
  26:   
  27:  //After our PDF has been submited we get a GUID which we store in an array to use with our letter below.
  28:  //Note you can attach multiple PDF's to your letter by adding to this array.
  29:  $fileGuidArray =  array($uploadedFileResult->UploadDocumentResult->FileCreatedGUID);
  30:   
  31:   
  32:  // ********** The actual Letter.
  33:  // We put an address together
  34:  $address->ReceiverName = "Tom Smith";
  35:  $address->ReceiverAddressLine1 = "Line 1 ";
  36:  $address->ReceiverAddressLine2 = "Line 2";
  37:  $address->ReceiverAddressTownCityOrLine3 = "My Town";
  38:  $address->ReceiverAddressCountyStateOrLine4 = "County";
  39:  $address->ReceiverAddressPostCode = "ZZ1 2ZZ";
  40:   
  41:  // Add the address to an address array
  42:  $addressArray = array($address);
  43:   
  44:  //Add the address array to our letter
  45:  $letter->Addresses = $addressArray ;
  46:   
  47:  //Setup our postage options (use the LetterPosting API for these values 
  48:  //or ask us and we will be happy to provide the values based on your needs)
  49:   
  50:   
  51:  // 1=UK , 240=US for a list of more please refer to country.csv file from the API section of our website.
  52:  $letter->ReceiverCountryCode = 1; 
  53:   
  54:  $letter->IncludeSenderAddressOnEnvelope = true;
  55:  $letter->Postage = 3; //UK First Class
  56:  $letter->Paper = 1;  // B&W Single Sides print
  57:  $letter->Extras = 0;
  58:  $letter->Envelope = 1; //Simple DL envelope
  59:  $letter->Pages = 2;
  60:  $letter->FileAttachementGUIDs = $fileGuidArray;
  61:   
  62:  $letter->SenderAddress = "Peter Smith\nMy Town";
  63:   
  64:  //This is the cover letter of your letter. Leave this null if you do not want a cover letter
  65:  //You may use formatted HTML in here to format your letter.
  66:  $letter->LetterBody = "<p>Hi Tom,</p> <p>This is a letter sent from PHP!</p>";
  67:   
  68:   
  69:  $parameters->letterForPosting = $letter;
  70:   
  71:  // Your PC2Paper username and password
  72:  $parameters->username = $pc2paperUsername;
  73:  $parameters->password = $pc2paperPassword;
  74:   
  75:  $result = $client->SendSubmitLetterForPosting($parameters);
  76:   
  77:  // The return object will give you the following
  78:  // $result->SendSubmitLetterForPostingResult->CostOfLetter
  79:  // $result->SendSubmitLetterForPostingResult->ErrorMessages  (any errors that took place)
  80:  // $result->SendSubmitLetterForPostingResult->FundsLeftInYourAccount
  81:  // $result->SendSubmitLetterForPostingResult->LetterId
  82:  // $result->SendSubmitLetterForPostingResult->Status (OK if everything went ok or ERROR, if error check the ErrorMessages array)
  83:   
  84:   
  85:  print_r($result);
  86:  ?>