Skip to content

Instantly share code, notes, and snippets.

@krisives
Created August 21, 2013 22:30
Show Gist options
  • Save krisives/6301084 to your computer and use it in GitHub Desktop.
Save krisives/6301084 to your computer and use it in GitHub Desktop.
<?php
/**
* Sample code for the CreateShipment Canada Post service.
*
* The CreateShipment service is used to initiate generation of a shipping label
* by providing shipment details. Use of this service indicates an intention to
* pay for shipment of an item.
*
* This sample is configured to access the Developer Program sandbox environment.
* Use your development key username and password for the web service credentials.
*
**/
// Your username and password are imported from the following file
// CPCWS_SOAP_Shipping_PHP_Samples\SOAP\shipping\user.ini
$userProperties = parse_ini_file(realpath(dirname($_SERVER['SCRIPT_FILENAME'])) . '/../user.ini');
$wsdl = realpath(dirname($_SERVER['SCRIPT_FILENAME'])) . '/../../wsdl/shipment.wsdl';
$hostName = 'ct.soa-gw.canadapost.ca';
// SOAP URI
$location = 'https://' . $hostName . '/rs/soap/shipment/v3';
// SSL Options
$opts = array('ssl' =>
array(
'verify_peer'=> false,
'cafile' => realpath(dirname($_SERVER['SCRIPT_FILENAME'])) . '/../../../third-party/cert/cacert.pem',
'CN_match' => $hostName
)
);
$ctx = stream_context_create($opts);
$client = new SoapClient($wsdl,array('location' => $location, 'features' => SOAP_SINGLE_ELEMENT_ARRAYS, 'stream_context' => $ctx));
// Set WS Security UsernameToken
$WSSENS = 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd';
$usernameToken = new stdClass();
$usernameToken->Username = new SoapVar($userProperties['username'], XSD_STRING, null, null, null, $WSSENS);
$usernameToken->Password = new SoapVar($userProperties['password'], XSD_STRING, null, null, null, $WSSENS);
$content = new stdClass();
$content->UsernameToken = new SoapVar($usernameToken, SOAP_ENC_OBJECT, null, null, null, $WSSENS);
$header = new SOAPHeader($WSSENS, 'Security', $content);
$client->__setSoapHeaders($header);
try {
$mailedBy = $userProperties['customerNumber'];
$groupId = '4326432';
$requestedShippingPoint = 'H2B1A0';
$mailingDate = '2012-10-24';
$contractId = '0040662521';
// Execute Request
$result = $client->__soapCall('CreateShipment', array(
'create-shipment-request' => array(
'locale' => 'FR',
'mailed-by' => $mailedBy,
'shipment' => array(
'group-id' => $groupId,
'requested-shipping-point' => $requestedShippingPoint,
'expected-mailing-date' => $mailingDate,
'delivery-spec' => array(
'service-code' => 'DOM.EP',
'sender' => array(
'name' => 'Bulma',
'company' => 'company',
'contact-phone' => 'contact-phone',
'address-details' => array(
'address-line-1' => '502 MAIN ST N',
'city' => 'MONTREAL',
'prov-state' => 'QC',
'country-code' => 'CA',
'postal-zip-code' => 'H2B1A0'
)
),
'destination' => array(
'name' => 'John Doe',
'company' => 'ACME Corp.',
'address-details' => array(
'address-line-1' => '123 Postal Drive',
'city' => 'Ottawa',
'prov-state' => 'ON',
'country-code' => 'CA',
'postal-zip-code' => 'K1P5Z9'
)
),
'options' => array(
'option' => array(
'option-code' => 'DC'
)
),
'parcel-characteristics' => array(
'weight' => 15,
'dimensions' => array(
'length' => 6,
'width' => 12,
'height' => 9
),
'unpackaged' => false,
'mailing-tube' => false
),
'notification' => array(
'email' => 'ryuko.saito@kubere.com',
'on-shipment' => true,
'on-exception' => false,
'on-delivery' => true
),
'print-preferences' => array(
'output-format' => '8.5x11'
),
'preferences' => array(
'show-packing-instructions' => true,
'show-postage-rate' => false,
'show-insured-value' => true
),
'settlement-info' => array(
'contract-id' => $contractId,
'intended-method-of-payment' => 'Account'
)
)
)
)
), NULL, NULL);
// Parse Response
if ( isset($result->{'shipment-info'}) ) {
echo 'Shipment Id: ' . $result->{'shipment-info'}->{'shipment-id'} . "\n";
echo 'Tracking Pin: ' . $result->{'shipment-info'}->{'tracking-pin'} . "\n";
foreach ( $result->{'shipment-info'}->{'artifacts'}->{'artifact'} as $artifact ) {
echo 'Artifact Id: ' . $artifact->{'artifact-id'} . "\n";
echo 'Page Index: ' . $artifact->{'page-index'} . "\n\n";
}
} else {
foreach ( $result->{'messages'}->{'message'} as $message ) {
echo 'Error Code: ' . $message->code . "\n";
echo 'Error Msg: ' . $message->description . "\n\n";
}
}
} catch (SoapFault $exception) {
echo 'Fault Code: ' . trim($exception->faultcode) . "\n";
echo 'Fault Reason: ' . trim($exception->getMessage()) . "\n";
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment