DHL-PHP-SDK  v0.4
A wrapper for the DHL-XML-API Version 2
BusinessShipment.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Petschko\DHL;
4 
16 use Exception;
17 use SoapClient;
18 use SoapHeader;
19 use stdClass;
20 
26 class BusinessShipment extends Version {
30  const DHL_WSDL_LIB_URL = 'https://cig.dhl.de/cig-wsdls/com/dpdhl/wsdl/geschaeftskundenversand-api/';
31 
35  const DHL_SOAP_HEADER_URI = 'http://dhl.de/webservice/cisbase';
36 
40  const DHL_SANDBOX_URL = 'https://cig.dhl.de/services/sandbox/soap';
41 
45  const DHL_PRODUCTION_URL = 'https://cig.dhl.de/services/production/soap';
46 
50  const NEWEST_VERSION = '2.2';
51 
55  const RESPONSE_TYPE_URL = 'URL';
56 
60  const RESPONSE_TYPE_B64 = 'B64';
61 
65  const MAX_DHL_REQUESTS = 30;
66 
67  // System-Fields
73  private $soapClient = null;
74 
80  private $errors = array();
81 
82  // Setting-Fields
88  private $test;
89 
95  private $log = false;
96 
97  // Object-Fields
105  private $credentials;
106 
114  private $shipmentDetails;
115 
125  private $service = null;
126 
136  private $bank = null;
137 
145  private $sender = null;
146 
154  private $receiver = null;
155 
165  private $returnReceiver = null;
166 
176  private $exportDocument = null;
177 
178  // Fields
189  private $sequenceNumber = '1';
190 
202  private $receiverEmail = null;
203 
213  private $printOnlyIfReceiverIsValid = null;
214 
225  private $labelResponseType = null;
226 
234  private $shipmentOrders = array();
235 
241  private $customAPIURL = null;
242 
253  public function __construct($credentials, $testMode = false, $version = null) {
254  // Set Version
255  if($version === null)
256  $version = self::NEWEST_VERSION;
257 
258  parent::__construct($version);
259 
260  // Set Test-Mode
261  $this->setTest((($testMode) ? true : false));
262 
263  // Set Credentials
264  if($this->isTest()) {
265  $c = new Credentials($testMode);
266  $c->setApiUser($credentials->getApiUser());
267  $c->setApiPassword($credentials->getApiPassword());
268 
269  $credentials = $c;
270  }
271 
272  $this->setCredentials($credentials);
273 
274  // @deprecated Set Shipment-Class for Backward-Compatibility todo remove in newer versions
275  $this->shipmentDetails = new ShipmentDetails($credentials->getEkp(10) . '0101');
276  }
277 
281  public function __destruct() {
282  parent::__destruct();
283  unset($this->soapClient);
284  unset($this->errors);
285  unset($this->test);
286  unset($this->log);
287  unset($this->credentials);
288  unset($this->shipmentDetails);
289  unset($this->service);
290  unset($this->bank);
291  unset($this->sender);
292  unset($this->receiver);
293  unset($this->returnReceiver);
294  unset($this->exportDocument);
295  unset($this->sequenceNumber);
296  unset($this->receiverEmail);
297  unset($this->printOnlyIfReceiverIsValid);
298  unset($this->labelResponseType);
299  unset($this->shipmentOrders);
300  unset($this->customAPIURL);
301  }
302 
308  protected function getAPIUrl() {
309  // Use own API-URL if set
310  if($this->getCustomAPIURL() !== null)
311  return $this->getCustomAPIURL();
312 
313  return self::DHL_WSDL_LIB_URL . $this->getVersion() . '/geschaeftskundenversand-api-' . $this->getVersion() . '.wsdl';
314  }
315 
321  private function getSoapClient() {
322  if($this->soapClient === null)
323  $this->buildSoapClient();
324 
325  return $this->soapClient;
326  }
327 
333  public function getLastXML() {
334  if($this->soapClient === null)
335  return null;
336 
337  return $this->getSoapClient()->__getLastRequest();
338  }
339 
345  private function setSoapClient($soapClient) {
346  $this->soapClient = $soapClient;
347  }
348 
354  public function getErrors() {
355  return $this->errors;
356  }
357 
363  public function setErrors($errors) {
364  $this->errors = $errors;
365  }
366 
372  private function addError($error) {
373  $this->errors[] = $error;
374  }
375 
381  private function isTest() {
382  return $this->test;
383  }
384 
390  private function setTest($test) {
391  $this->test = $test;
392  }
393 
399  public function isLog() {
400  return $this->log;
401  }
402 
408  public function setLog($log) {
409  $this->log = $log;
410  }
411 
417  private function getCredentials() {
418  return $this->credentials;
419  }
420 
426  public function setCredentials($credentials) {
427  $this->credentials = $credentials;
428  }
429 
437  public function getShipmentDetails() {
438  trigger_error(
439  '[DHL-PHP-SDK]: ' . __CLASS__ . '->' . __METHOD__ .
440  ' These details belong to the `ShipmentOrder` Object, please do/get them there and assign the' .
441  ' `ShipmentOrder` Object to this Object by using `addShipmentOrder($shipmentOrder)` on this instance!',
442  E_USER_DEPRECATED
443  );
444 
445  return $this->shipmentDetails;
446  }
447 
455  public function setShipmentDetails($shipmentDetails) {
456  trigger_error(
457  '[DHL-PHP-SDK]: ' . __CLASS__ . '->' . __METHOD__ .
458  ' These details belong to the `ShipmentOrder` Object, please do/get them there and assign the' .
459  ' `ShipmentOrder` Object to this Object by using `addShipmentOrder($shipmentOrder)` on this instance!',
460  E_USER_DEPRECATED
461  );
462 
463  $this->shipmentDetails = $shipmentDetails;
464  }
465 
473  public function getService() {
474  trigger_error(
475  '[DHL-PHP-SDK]: ' . __CLASS__ . '->' . __METHOD__ .
476  ' These details belong to the `ShipmentDetails` Object, please do/get them there and assign the' .
477  ' `ShipmentDetails` Object to the `ShipmentOrder` Object by using `setShipmentDetails($shipmentDetails)`' .
478  ' on the Shipment instance!',
479  E_USER_DEPRECATED
480  );
481 
482  return $this->service;
483  }
484 
492  public function setService($service) {
493  trigger_error(
494  '[DHL-PHP-SDK]: ' . __CLASS__ . '->' . __METHOD__ .
495  ' These details belong to the `ShipmentDetails` Object, please do/get them there and assign the' .
496  ' `ShipmentDetails` Object to the `ShipmentOrder` Object by using `setShipmentDetails($shipmentDetails)`' .
497  ' on the Shipment instance!',
498  E_USER_DEPRECATED
499  );
500 
501  $this->service = $service;
502  }
503 
511  public function getBank() {
512  trigger_error(
513  '[DHL-PHP-SDK]: ' . __CLASS__ . '->' . __METHOD__ .
514  ' These details belong to the `ShipmentDetails` Object, please do/get them there and assign the' .
515  ' `ShipmentDetails` Object to the `ShipmentOrder` Object by using `setShipmentDetails($shipmentDetails)`' .
516  ' on the Shipment instance!',
517  E_USER_DEPRECATED
518  );
519 
520  return $this->bank;
521  }
522 
530  public function setBank($bank) {
531  trigger_error(
532  '[DHL-PHP-SDK]: ' . __CLASS__ . '->' . __METHOD__ .
533  ' These details belong to the `ShipmentDetails` Object, please do/get them there and assign the' .
534  ' `ShipmentDetails` Object to the `ShipmentOrder` Object by using `setShipmentDetails($shipmentDetails)`' .
535  ' on the Shipment instance!',
536  E_USER_DEPRECATED
537  );
538 
539  $this->bank = $bank;
540  }
541 
549  public function getSender() {
550  trigger_error(
551  '[DHL-PHP-SDK]: ' . __CLASS__ . '->' . __METHOD__ .
552  ' These details belong to the `ShipmentOrder` Object, please do/get them there and assign the' .
553  ' `ShipmentOrder` Object to this Object by using `addShipmentOrder($shipmentOrder)` on this instance!',
554  E_USER_DEPRECATED
555  );
556 
557  return $this->sender;
558  }
559 
567  public function setSender($sender) {
568  trigger_error(
569  '[DHL-PHP-SDK]: ' . __CLASS__ . '->' . __METHOD__ .
570  ' These details belong to the `ShipmentOrder` Object, please do/get them there and assign the' .
571  ' `ShipmentOrder` Object to this Object by using `addShipmentOrder($shipmentOrder)` on this instance!',
572  E_USER_DEPRECATED
573  );
574 
575  $this->sender = $sender;
576  }
577 
585  public function getReceiver() {
586  trigger_error(
587  '[DHL-PHP-SDK]: ' . __CLASS__ . '->' . __METHOD__ .
588  ' These details belong to the `ShipmentOrder` Object, please do/get them there and assign the' .
589  ' `ShipmentOrder` Object to this Object by using `addShipmentOrder($shipmentOrder)` on this instance!',
590  E_USER_DEPRECATED
591  );
592 
593  return $this->receiver;
594  }
595 
603  public function setReceiver($receiver) {
604  trigger_error(
605  '[DHL-PHP-SDK]: ' . __CLASS__ . '->' . __METHOD__ .
606  ' These details belong to the `ShipmentOrder` Object, please do/get them there and assign the' .
607  ' `ShipmentOrder` Object to this Object by using `addShipmentOrder($shipmentOrder)` on this instance!',
608  E_USER_DEPRECATED
609  );
610 
611  $this->receiver = $receiver;
612  }
613 
623  public function getReturnReceiver() {
624  trigger_error(
625  '[DHL-PHP-SDK]: ' . __CLASS__ . '->' . __METHOD__ .
626  ' These details belong to the `ShipmentOrder` Object, please do/get them there and assign the' .
627  ' `ShipmentOrder` Object to this Object by using `addShipmentOrder($shipmentOrder)` on this instance!',
628  E_USER_DEPRECATED
629  );
630 
631  return $this->returnReceiver;
632  }
633 
643  public function setReturnReceiver($returnReceiver) {
644  trigger_error(
645  '[DHL-PHP-SDK]: ' . __CLASS__ . '->' . __METHOD__ .
646  ' These details belong to the `ShipmentOrder` Object, please do/get them there and assign the' .
647  ' `ShipmentOrder` Object to this Object by using `addShipmentOrder($shipmentOrder)` on this instance!',
648  E_USER_DEPRECATED
649  );
650 
651  $this->returnReceiver = $returnReceiver;
652  }
653 
661  public function getExportDocument() {
662  trigger_error(
663  '[DHL-PHP-SDK]: ' . __CLASS__ . '->' . __METHOD__ .
664  ' These details belong to the `ShipmentOrder` Object, please do/get them there and assign the' .
665  ' `ShipmentOrder` Object to this Object by using `addShipmentOrder($shipmentOrder)` on this instance!',
666  E_USER_DEPRECATED
667  );
668 
669  return $this->exportDocument;
670  }
671 
679  public function setExportDocument($exportDocument) {
680  trigger_error(
681  '[DHL-PHP-SDK]: ' . __CLASS__ . '->' . __METHOD__ .
682  ' These details belong to the `ShipmentOrder` Object, please do/get them there and assign the' .
683  ' `ShipmentOrder` Object to this Object by using `addShipmentOrder($shipmentOrder)` on this instance!',
684  E_USER_DEPRECATED
685  );
686 
687  $this->exportDocument = $exportDocument;
688  }
689 
697  public function getSequenceNumber() {
698  trigger_error(
699  '[DHL-PHP-SDK]: ' . __CLASS__ . '->' . __METHOD__ .
700  ' These details belong to the `ShipmentOrder` Object, please do/get them there and assign the' .
701  ' `ShipmentOrder` Object to this Object by using `addShipmentOrder($shipmentOrder)` on this instance!',
702  E_USER_DEPRECATED
703  );
704 
705  return $this->sequenceNumber;
706  }
707 
715  public function setSequenceNumber($sequenceNumber) {
716  trigger_error(
717  '[DHL-PHP-SDK]: ' . __CLASS__ . '->' . __METHOD__ .
718  ' These details belong to the `ShipmentOrder` Object, please do/get them there and assign the' .
719  ' `ShipmentOrder` Object to this Object by using `addShipmentOrder($shipmentOrder)` on this instance!',
720  E_USER_DEPRECATED
721  );
722 
723  $this->sequenceNumber = $sequenceNumber;
724  }
725 
733  public function getReceiverEmail() {
734  trigger_error('[DHL-PHP-SDK]: Called deprecated method ' . __METHOD__ . ' in class ' . __CLASS__ .
735  '. The notification E-Mail (or receiver E-Mail) was moved into the ShipmentDetail class!' .
736  ' Please use the new function, this here will removed in the future!', E_USER_DEPRECATED);
737 
738  return $this->receiverEmail;
739  }
740 
748  public function setReceiverEmail($receiverEmail) {
749  trigger_error('[DHL-PHP-SDK]: Called deprecated method ' . __METHOD__ . ' in class ' . __CLASS__ .
750  '. The notification E-Mail (or receiver E-Mail) was moved into the ShipmentDetail class!' .
751  ' Please use the new function, this here will removed in the future!', E_USER_DEPRECATED);
752 
753  $this->receiverEmail = $receiverEmail;
754  }
755 
763  public function getPrintOnlyIfReceiverIsValid() {
764  trigger_error(
765  '[DHL-PHP-SDK]: ' . __CLASS__ . '->' . __METHOD__ .
766  ' These details belong to the `ShipmentOrder` Object, please do/get them there and assign the' .
767  ' `ShipmentOrder` Object to this Object by using `addShipmentOrder($shipmentOrder)` on this instance!',
768  E_USER_DEPRECATED
769  );
770 
771  return $this->printOnlyIfReceiverIsValid;
772  }
773 
783  public function setPrintOnlyIfReceiverIsValid($printOnlyIfReceiverIsValid) {
784  trigger_error(
785  '[DHL-PHP-SDK]: ' . __CLASS__ . '->' . __METHOD__ .
786  ' These details belong to the `ShipmentOrder` Object, please do/get them there and assign the' .
787  ' `ShipmentOrder` Object to this Object by using `addShipmentOrder($shipmentOrder)` on this instance!',
788  E_USER_DEPRECATED
789  );
790 
791  $this->printOnlyIfReceiverIsValid = $printOnlyIfReceiverIsValid;
792  }
793 
799  public function getLabelResponseType() {
800  return $this->labelResponseType;
801  }
802 
808  public function setLabelResponseType($labelResponseType) {
809  $this->labelResponseType = $labelResponseType;
810  }
811 
817  public function getShipmentOrders() {
818  return $this->shipmentOrders;
819  }
820 
826  public function setShipmentOrders($shipmentOrders) {
827  if(! is_array($shipmentOrders)) {
828  trigger_error(
829  '[DHL-PHP-SDK]: The type of $shipmentOrders is NOT an array, but is required to set as array! Called method ' .
830  __METHOD__ . ' in class ' . __CLASS__,
831  E_USER_ERROR
832  );
833  $this->addError(__METHOD__ . ': Non-Array value given');
834 
835  return;
836  }
837 
838  $this->shipmentOrders = $shipmentOrders;
839  }
840 
846  public function addShipmentOrder($shipmentOrder) {
847  $this->shipmentOrders[] = $shipmentOrder;
848  }
849 
853  public function clearShipmentOrders() {
854  $this->setShipmentOrders(array());
855  }
856 
862  public function countShipmentOrders() {
863  return count($this->getShipmentOrders());
864  }
865 
871  public function getCustomAPIURL() {
872  return $this->customAPIURL;
873  }
874 
880  public function setCustomAPIURL($customAPIURL) {
881  $this->customAPIURL = $customAPIURL;
882  }
883 
891  private function checkRequestCount($array, $action, $maxReq = self::MAX_DHL_REQUESTS) {
892  $count = count($array);
893 
894  if($count > self::MAX_DHL_REQUESTS)
895  $this->addError('There are only ' . $maxReq . ' Request/s for one call allowed for the action "'
896  . $action . '"! You tried to request ' . $count . ' ones');
897  }
898 
905  private function log($message, $errorLevel = E_USER_NOTICE) {
906  if($this->isLog()) {
907  if(is_array($message) || is_object($message))
908  error_log(print_r($message, true), $errorLevel);
909  else
910  error_log($message, $errorLevel);
911  }
912  }
913 
919  private function buildAuthHeader() {
920  $auth_params = array(
921  'user' => $this->getCredentials()->getUser(),
922  'signature' => $this->getCredentials()->getSignature(),
923  'type' => 0
924  );
925 
926  return new SoapHeader(self::DHL_SOAP_HEADER_URI, 'Authentification', $auth_params);
927  }
928 
932  private function buildSoapClient() {
933  $header = $this->buildAuthHeader();
934 
935  if($this->isTest())
936  $location = self::DHL_SANDBOX_URL;
937  else
938  $location = self::DHL_PRODUCTION_URL;
939 
940  $auth_params = array(
941  'login' => $this->getCredentials()->getApiUser(),
942  'password' => $this->getCredentials()->getApiPassword(),
943  'location' => $location,
944  'trace' => 1
945  );
946 
947  $this->log($auth_params);
948  $this->setSoapClient(new SoapClient($this->getAPIUrl(), $auth_params));
949  $this->getSoapClient()->__setSoapHeaders($header);
950  $this->log($this->getSoapClient());
951  }
952 
961  public function getVersion($viaSOAP = false, $getBuildNumber = false, $returnAsArray = false) {
962  if(! $viaSOAP) {
963  if($returnAsArray)
964  return array(
965  'mayor' => parent::getMayor(),
966  'minor' => parent::getMinor()
967  );
968  else
969  return parent::getVersion();
970  }
971 
972  switch($this->getMayor()) {
973  case 1:
974  trigger_error('[DHL-PHP-SDK]: Called Version 1 Method: ' . __METHOD__ . ' is incomplete (does nothing)!', E_USER_WARNING);
975  $this->addError('Version 1 SOAP-Method "' . __METHOD__ . '" is not implemented or removed!');
976 
977  return false;
978  case 2:
979  default:
980  $data = $this->getVersionClass();
981  }
982 
983  try {
984  $response = $this->sendGetVersionRequest($data);
985  } catch(Exception $e) {
986  $this->addError($e->getMessage());
987 
988  return false;
989  }
990 
991  if(is_soap_fault($response)) {
992  $this->addError($response->faultstring);
993 
994  return false;
995  } else {
996  if($returnAsArray)
997  return array(
998  'mayor' => $response->Version->majorRelease,
999  'minor' => $response->Version->minorRelease,
1000  'build' => $response->Version->build
1001  );
1002  else
1003  return $response->Version->majorRelease . '.' . $response->Version->minorRelease .
1004  (($getBuildNumber) ? '.' . $response->Version->build : '');
1005  }
1006  }
1007 
1014  private function sendGetVersionRequest($data) {
1015  return $this->getSoapClient()->getVersion($data);
1016  }
1017 
1024  private function sendDoManifestRequest($data) {
1025  switch($this->getMayor()) {
1026  case 1:
1027  return $this->getSoapClient()->DoManifestTD($data);
1028  case 2:
1029  default:
1030  return $this->getSoapClient()->doManifest($data);
1031  }
1032  }
1033 
1040  public function doManifest($shipmentNumbers) {
1041  switch($this->getMayor()) {
1042  case 1:
1043  $data = $this->createDoManifestClass_v1($shipmentNumbers);
1044  break;
1045  case 2:
1046  default:
1047  $data = $this->createDoManifestClass_v2($shipmentNumbers);
1048  }
1049 
1050  try {
1051  $response = $this->sendDoManifestRequest($data);
1052  } catch(Exception $e) {
1053  $this->addError($e->getMessage());
1054 
1055  return false;
1056  }
1057 
1058  if(is_soap_fault($response)) {
1059  $this->addError($response->faultstring);
1060 
1061  return false;
1062  } else
1063  return new Response($this->getVersion(), $response);
1064  }
1065 
1074  private function createDoManifestClass_v1($shipmentNumbers) {
1075  trigger_error('[DHL-PHP-SDK]: Version 1 Methods are deprecated and will removed soon (Called method ' . __METHOD__ . ')!', E_USER_DEPRECATED);
1076  trigger_error('[DHL-PHP-SDK]: Called Version 1 Method: ' . __METHOD__ . ' is incomplete (does nothing)!', E_USER_WARNING);
1077 
1078  $data = new StdClass;
1079 
1080  return $data;
1081  }
1082 
1089  private function createDoManifestClass_v2($shipmentNumbers) {
1090  $data = new StdClass;
1091 
1092  $data->Version = $this->getVersionClass();
1093 
1094  if(is_array($shipmentNumbers)) {
1095  $this->checkRequestCount($shipmentNumbers, 'doManifest');
1096 
1097  foreach($shipmentNumbers as $key => &$number)
1098  $data->shipmentNumber[$key] = $number;
1099  } else
1100  $data->shipmentNumber = $shipmentNumbers;
1101 
1102  return $data;
1103  }
1104 
1112  public function getManifest($manifestDate, $useIntTime = false) {
1113  if($useIntTime) {
1114  // Convert to Date-Format for DHL
1115  $oldDate = $manifestDate;
1116  $manifestDate = date('Y-m-d', $manifestDate);
1117 
1118  if($manifestDate === false) {
1119  $this->addError('Could not convert given time() value "' . $oldDate . '" to YYYY-MM-DD... Called method: ' . __METHOD__);
1120 
1121  return false;
1122  }
1123 
1124  unset($oldDate);
1125  }
1126 
1127  switch($this->getMayor()) {
1128  case 1:
1129  trigger_error('[DHL-PHP-SDK]: Called Version 1 Method: ' . __METHOD__ . ' is incomplete (does nothing)!', E_USER_WARNING);
1130  $this->addError('Version 1 Method "' . __METHOD__ . '" is not implemented or removed!');
1131 
1132  return false;
1133  case 2:
1134  default:
1135  $data = $this->createGetManifestClass_v2($manifestDate);
1136  }
1137 
1138  try {
1139  $response = $this->sendGetManifestRequest($data);
1140  } catch(Exception $e) {
1141  $this->addError($e->getMessage());
1142 
1143  return false;
1144  }
1145 
1146  if(is_soap_fault($response)) {
1147  $this->addError($response->faultstring);
1148 
1149  return false;
1150  } else
1151  return new Response($this->getVersion(), $response);
1152  }
1153 
1160  private function createGetManifestClass_v2($manifestDate) {
1161  $data = new StdClass;
1162 
1163  if(is_array($manifestDate))
1164  $this->addError('You can only request 1 date on getManifest - multiple requests in 1 call are not allowed here');
1165 
1166  $data->Version = $this->getVersionClass();
1167  $data->manifestDate = $manifestDate;
1168 
1169  return $data;
1170  }
1171 
1178  private function sendGetManifestRequest($data) {
1179  return $this->getSoapClient()->getManifest($data);
1180  }
1181 
1188  private function sendCreateRequest($data) {
1189  switch($this->getMayor()) {
1190  case 1:
1191  return $this->getSoapClient()->CreateShipmentDD($data);
1192  case 2:
1193  default:
1194  return $this->getSoapClient()->createShipmentOrder($data);
1195  }
1196  }
1197 
1205  public function createShipment() {
1206  return $this->createShipmentOrder();
1207  }
1208 
1214  public function createShipmentOrder() {
1215  switch($this->getMayor()) {
1216  case 1:
1217  $data = $this->createShipmentClass_v1();
1218  break;
1219  case 2:
1220  default:
1221  if($this->countShipmentOrders() < 1)
1222  $data = $this->createShipmentClass_v2_legacy();
1223  else
1224  $data = $this->createShipmentClass_v2();
1225  }
1226 
1227  $response = null;
1228 
1229  // Create Shipment
1230  try {
1231  $response = $this->sendCreateRequest($data);
1232  } catch(Exception $e) {
1233  $this->addError($e->getMessage());
1234 
1235  return false;
1236  }
1237 
1238  if(is_soap_fault($response)) {
1239  $this->addError($response->faultstring);
1240 
1241  return false;
1242  } else
1243  return new Response($this->getVersion(), $response);
1244  }
1245 
1253  private function createShipmentClass_v1() {
1254  trigger_error('[DHL-PHP-SDK]: Version 1 Methods are deprecated and will removed soon (Called method ' . __METHOD__ . ')!', E_USER_DEPRECATED);
1255  trigger_error('[DHL-PHP-SDK]: Called Version 1 Method: ' . __METHOD__ . ' is incomplete (does nothing)!', E_USER_WARNING);
1256 
1257  $data = new StdClass;
1258 
1259  return $data;
1260  }
1261 
1267  private function createShipmentClass_v2() {
1268  $shipmentOrders = $this->getShipmentOrders();
1269 
1270  $this->checkRequestCount($shipmentOrders, 'createShipmentClass');
1271 
1272  $data = new StdClass;
1273  $data->Version = $this->getVersionClass();
1274 
1275  foreach($shipmentOrders as $key => &$shipmentOrder) {
1279  // Set global response-type if none is defined in shipment
1280  if($shipmentOrder->getLabelResponseType() === null && $this->getLabelResponseType() !== null)
1281  $shipmentOrder->setLabelResponseType($this->getLabelResponseType());
1282 
1283  $data->ShipmentOrder[$key] = $shipmentOrder->getShipmentOrderClass_v2(); // todo test
1284  }
1285 
1286  return $data;
1287  }
1288 
1296  private function createShipmentClass_v2_legacy() {
1297  trigger_error(
1298  '[DHL-PHP-SDK]: ' . __CLASS__ . '->' . __METHOD__ .
1299  ' This method was called for Backward-Compatibility, please create `ShipmentOrder` Objects' .
1300  ' and assign them with `addShipmentOrder($shipmentOrder)` on this instance.',
1301  E_USER_DEPRECATED
1302  );
1303 
1304  // Set old values
1305  $this->getShipmentDetails()->setService($this->getService());
1306  $this->getShipmentDetails()->setBank($this->getBank());
1307 
1308  // Create class
1309  $data = new StdClass;
1310  $data->Version = $this->getVersionClass();
1311  $data->ShipmentOrder = new StdClass;
1312  $data->ShipmentOrder->sequenceNumber = $this->getSequenceNumber();
1313 
1314  // Shipment
1315  $data->ShipmentOrder->Shipment = new StdClass;
1316  $data->ShipmentOrder->Shipment->ShipmentDetails = $this->getShipmentDetails()->getShipmentDetailsClass_v2();
1317 
1318  // Notification
1319  $email = null; // Check for backward compatibility
1320  if($this->getShipmentDetails()->getNotificationEmail() === null && $this->receiverEmail !== null)
1321  $email = $this->getReceiverEmail(); // Use old E-Mail implementation for BC
1322 
1323  if($email !== null) {
1324  $data->ShipmentOrder->Shipment->ShipmentDetails->Notification = new StdClass;
1325  $data->ShipmentOrder->Shipment->ShipmentDetails->Notification->recipientEmailAddress = $email;
1326  }
1327 
1328  // Shipper
1329  $data->ShipmentOrder->Shipment->Shipper = $this->getSender()->getClass_v2();
1330 
1331  // Receiver
1332  $data->ShipmentOrder->Shipment->Receiver = $this->getReceiver()->getClass_v2();
1333 
1334  // Return-Receiver
1335  if($this->getReturnReceiver() !== null)
1336  $data->ShipmentOrder->Shipment->ReturnReceiver = $this->getReturnReceiver()->getClass_v2();
1337 
1338  // Export-Document
1339  if($this->getExportDocument() !== null) {
1340  try {
1341  $data->ShipmentOrder->Shipment->ExportDocument = $this->getExportDocument()->getExportDocumentClass_v2();
1342  } catch(Exception $e) {
1343  $this->addError($e->getMessage());
1344  }
1345  }
1346 
1347  // Other Settings
1348  if($this->getPrintOnlyIfReceiverIsValid() !== null) {
1349  $data->ShipmentOrder->PrintOnlyIfCodeable = new StdClass;
1350  $data->ShipmentOrder->PrintOnlyIfCodeable->active = (int) $this->getPrintOnlyIfReceiverIsValid();
1351  }
1352  if($this->getLabelResponseType() !== null)
1353  $data->ShipmentOrder->labelResponseType = $this->getLabelResponseType();
1354 
1355  return $data;
1356  }
1357 
1364  private function sendDeleteRequest($data) {
1365  switch($this->getMayor()) {
1366  case 1:
1367  return $this->getSoapClient()->DeleteShipmentDD($data);
1368  case 2:
1369  default:
1370  return $this->getSoapClient()->deleteShipmentOrder($data);
1371  }
1372  }
1373 
1382  public function deleteShipment($shipmentNumbers) {
1383  return $this->deleteShipmentOrder($shipmentNumbers);
1384  }
1385 
1392  public function deleteShipmentOrder($shipmentNumbers) {
1393  switch($this->getMayor()) {
1394  case 1:
1395  $data = $this->createDeleteClass_v1($shipmentNumbers);
1396  break;
1397  case 2:
1398  default:
1399  $data = $this->createDeleteClass_v2($shipmentNumbers);
1400  }
1401 
1402  try {
1403  $response = $this->sendDeleteRequest($data);
1404  } catch(Exception $e) {
1405  $this->addError($e->getMessage());
1406 
1407  return false;
1408  }
1409 
1410  if(is_soap_fault($response)) {
1411  $this->addError($response->faultstring);
1412 
1413  return false;
1414  } else
1415  return new Response($this->getVersion(), $response);
1416  }
1417 
1426  private function createDeleteClass_v1($shipmentNumbers) {
1427  trigger_error('[DHL-PHP-SDK]: Version 1 Methods are deprecated and will removed soon (Called method ' . __METHOD__ . ')!', E_USER_DEPRECATED);
1428  trigger_error('[DHL-PHP-SDK]: Called Version 1 Method: ' . __METHOD__ . ' is incomplete (does nothing)!', E_USER_WARNING);
1429 
1430  $data = new StdClass;
1431 
1432  return $data;
1433  }
1434 
1441  private function createDeleteClass_v2($shipmentNumbers) {
1442  $data = new StdClass;
1443 
1444  $data->Version = $this->getVersionClass();
1445 
1446  if(is_array($shipmentNumbers)) {
1447  $this->checkRequestCount($shipmentNumbers, 'deleteShipmentOrder');
1448 
1449  foreach($shipmentNumbers as $key => &$number)
1450  $data->shipmentNumber[$key] = $number;
1451  } else
1452  $data->shipmentNumber = $shipmentNumbers;
1453 
1454  return $data;
1455  }
1456 
1463  private function sendGetLabelRequest($data) {
1464  switch($this->getMayor()) {
1465  case 1:
1466  return $this->getSoapClient()->getLabelDD($data);
1467  case 2:
1468  default:
1469  return $this->getSoapClient()->getLabel($data);
1470  }
1471  }
1472 
1481  public function getShipmentLabel($shipmentNumbers) {
1482  return $this->getLabel($shipmentNumbers);
1483  }
1484 
1491  public function getLabel($shipmentNumbers) {
1492  switch($this->getMayor()) {
1493  case 1:
1494  $data = $this->getLabelClass_v1($shipmentNumbers);
1495  break;
1496  case 2:
1497  default:
1498  $data = $this->getLabelClass_v2($shipmentNumbers);
1499  }
1500 
1501  try {
1502  $response = $this->sendGetLabelRequest($data);
1503  } catch(Exception $e) {
1504  $this->addError($e->getMessage());
1505 
1506  return false;
1507  }
1508 
1509  if(is_soap_fault($response)) {
1510  $this->addError($response->faultstring);
1511 
1512  return false;
1513  } else
1514  return new Response($this->getVersion(), $response);
1515  }
1516 
1525  private function getLabelClass_v1($shipmentNumbers) {
1526  trigger_error('[DHL-PHP-SDK]: Version 1 Methods are deprecated and will removed soon (Called method ' . __METHOD__ . ')!', E_USER_DEPRECATED);
1527  trigger_error('[DHL-PHP-SDK]: Called Version 1 Method: ' . __METHOD__ . ' is incomplete (does nothing)!', E_USER_WARNING);
1528 
1529  $data = new StdClass;
1530 
1531  return $data;
1532  }
1533 
1540  private function getLabelClass_v2($shipmentNumbers) {
1541  $data = new StdClass;
1542 
1543  $data->Version = $this->getVersionClass();
1544 
1545  if(is_array($shipmentNumbers)) {
1546  $this->checkRequestCount($shipmentNumbers, 'getLabel');
1547 
1548  foreach($shipmentNumbers as $key => &$number)
1549  $data->shipmentNumber[$key] = $number;
1550  } else
1551  $data->shipmentNumber = $shipmentNumbers;
1552 
1553  if($this->getLabelResponseType() !== null)
1554  $data->labelResponseType = $this->getLabelResponseType();
1555 
1556  return $data;
1557  }
1558 
1565  private function sendGetExportDocRequest($data) {
1566  switch($this->getMayor()) {
1567  case 1:
1568  return $this->getSoapClient()->getExportDocDD($data);
1569  case 2:
1570  default:
1571  return $this->getSoapClient()->getExportDoc($data);
1572  }
1573  }
1574 
1581  public function getExportDoc($shipmentNumbers) {
1582  switch($this->getMayor()) {
1583  case 1:
1584  $data = $this->getExportDocClass_v1($shipmentNumbers);
1585  break;
1586  case 2:
1587  default:
1588  $data = $this->getExportDocClass_v2($shipmentNumbers);
1589  }
1590 
1591  try {
1592  $response = $this->sendGetExportDocRequest($data);
1593  } catch(Exception $e) {
1594  $this->addError($e->getMessage());
1595 
1596  return false;
1597  }
1598 
1599  if(is_soap_fault($response)) {
1600  $this->addError($response->faultstring);
1601 
1602  return false;
1603  } else
1604  return new Response($this->getVersion(), $response);
1605  }
1606 
1615  private function getExportDocClass_v1($shipmentNumbers) {
1616  trigger_error('[DHL-PHP-SDK]: Version 1 Methods are deprecated and will removed soon (Called method ' . __METHOD__ . ')!', E_USER_DEPRECATED);
1617  trigger_error('[DHL-PHP-SDK]: Called Version 1 Method: ' . __METHOD__ . ' is incomplete (does nothing)!', E_USER_WARNING);
1618 
1619  $data = new StdClass;
1620 
1621  return $data;
1622  }
1623 
1630  private function getExportDocClass_v2($shipmentNumbers) {
1631  $data = new StdClass;
1632 
1633  $data->Version = $this->getVersionClass();
1634 
1635  if(is_array($shipmentNumbers)) {
1636  $this->checkRequestCount($shipmentNumbers, 'getExportDoc');
1637 
1638  foreach($shipmentNumbers as $key => &$number)
1639  $data->shipmentNumber[$key] = $number;
1640  } else
1641  $data->shipmentNumber = $shipmentNumbers;
1642 
1643  if($this->getLabelResponseType() !== null)
1644  $data->exportDocResponseType = $this->getLabelResponseType();
1645 
1646  return $data;
1647  }
1648 
1654  public function validateShipment() {
1655  switch($this->getMayor()) {
1656  case 1:
1657  $data = null;
1658  break;
1659  case 2:
1660  default:
1661  $data = $this->createShipmentClass_v2();
1662  }
1663 
1664  try {
1665  $response = $this->sendValidateShipmentRequest($data);
1666  } catch(Exception $e) {
1667  $this->addError($e->getMessage());
1668 
1669  return false;
1670  }
1671 
1672  if(is_soap_fault($response)) {
1673  $this->addError($response->faultstring);
1674 
1675  return false;
1676  } else
1677  return new Response($this->getVersion(), $response);
1678  }
1679 
1687  private function sendValidateShipmentRequest($data) {
1688  switch($this->getMayor()) {
1689  case 1:
1690  throw new Exception(__FUNCTION__ . ': Method doesn\'t exists for Version 1!');
1691  case 2:
1692  default:
1693  return $this->getSoapClient()->validateShipment($data);
1694  }
1695  }
1696 }
setPrintOnlyIfReceiverIsValid($printOnlyIfReceiverIsValid)
$testMode
Definition: test.php:15
__construct($credentials, $testMode=false, $version=null)
getVersion($viaSOAP=false, $getBuildNumber=false, $returnAsArray=false)
getManifest($manifestDate, $useIntTime=false)
$version
Definition: test.php:17
setShipmentDetails($shipmentDetails)
$response
Definition: test.php:90
setLabelResponseType($labelResponseType)
$shipmentOrder
Definition: test.php:79