DHL return module in PHP


Deutsche Post offers a returned SOAP Web service, You can quickly generate the return labels for packages. These pdf-files using the class can quickly and easily collect and display.

Example call:

$dhlRetoure = new DhlRetoure();
$pdf  = $dhlRetoure->getRetourePdf($surname, $familyname, $street, $streetNumber, $zip, $city);
if($pdf){
$dhlRetoure->displayPdf($pdf);
}

DHL return class:

<?php

/**
* DO NOT DELETE
* User: Sebastian Viereck
* www.sebastianviereck.de
* Date: 10.06.13
*/

class DhlRetoure
{
private $username = "xxx";
private $password = "xxx";
private $portalId = "xxx";
private $deliveryName = "xxx";
private $end_point = "https://amsel.dpwn.net/abholportal/gw/lp/SoapConnector";

public function getRetourePdf($surname, $familyname, $street, $streetNumber, $zip, $city)
{
$xmlRequest = $this->getRequestXml($surname, $familyname, $street, $streetNumber, $zip, $city);
$response = $this->curlSoapRequest($xmlRequest);
if($response){
$pdf = $this->getPdfFromResponse($response);
}
return $pdf;
}
public function displayPdf($pdf){
header("Content-type: application/pdf");
echo $pdf;
}

private function getRequestXml($surname, $familyname, $street, $streetNumber, $zip, $city)
{
$request =
"<?xml version='1.0' encoding='UTF-8' ?>
<soapenv:Envelope xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/' xmlns:var='https://amsel.dpwn.net/abholportal/gw/lp/schema/1.0/var3bl'>
<soapenv:Header>
<wsse:Security soapenv:mustUnderstand='1' xmlns:wsse='http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd'>
<wsse:UsernameToken>
<wsse:Username>".$this->username."</wsse:Username>
<wsse:Password Type='http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText'>
".$this->password."
</wsse:Password>
</wsse:UsernameToken>
</wsse:Security>
</soapenv:Header>
<soapenv:Body>
<var:BookLabelRequest
portalId='".$this->portalId."'
deliveryName='".$this->deliveryName."'
shipmentReference='Shipment Reference'
customerReference='Customer Reference'
labelFormat='PDF'
senderName1='$surname'
senderName2='$familyname'
senderCareOfName='CareofName'
senderContactPhone=''
senderStreet='$street'
senderStreetNumber='$streetNumber'
senderBoxNumber=''
senderPostalCode='$zip'
senderCity='$city' />
</soapenv:Body>
</soapenv:Envelope>";
return $request;
}

/**
* @param $soap_request
* @return mixed
*/
private function curlSoapRequest($xmlRequest)
{
$header = array(
"Content-type: text/xml;charset=\"utf-8\"",
"Accept: text/xml",
"Cache-Control: no-cache",
"Pragma: no-cache",
"Content-length: " . strlen($xmlRequest),
);

$soap_do = curl_init();
curl_setopt($soap_do, CURLOPT_URL, $this->end_point);
curl_setopt($soap_do, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($soap_do, CURLOPT_TIMEOUT, 10);
curl_setopt($soap_do, CURLOPT_RETURNTRANSFER, true);
curl_setopt($soap_do, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($soap_do, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($soap_do, CURLOPT_POST, true);
curl_setopt($soap_do, CURLOPT_POSTFIELDS, $xmlRequest);
curl_setopt($soap_do, CURLOPT_HTTPHEADER, $header);
$response = curl_exec($soap_do);

if (!$response) {
$err = 'Curl error: ' . curl_error($soap_do);
}
else {
/* var_dump(htmlentities($response));*/
}
curl_close($soap_do);
return $response;
}

/**
* @param $response
* @return string
*/
private function getPdfFromResponse($response)
{
$xml = simplexml_load_string($response);
$ns = $xml->getNamespaces(true);
$soap = $xml->children($ns['env']);
$pdf = $soap->Body->children($ns['var3bl'])->BookLabelResponse->label;
$pdf = base64_decode($pdf);
return $pdf;
}
}

 

  1. Thank you, worked wonderfully.

    Small note for example:
    $dhlRetoure = new DhlRetoure();
    $PDF = $dhl->getRetourePdf($Su….
    should the variable name to be adjusted, otherwise it does not:
    $PDF = $dhlRetoure->getRetourePdf($Su….
    ;)
    Many greetings

  2. Great,

    right then, I've been looking for a day. The stupid side of DHL are there unfortunately no assistance…

    Greeting

  3. Hello Sebastian, class and thank you! The question I have just, what I have to wear a?!

    private $username = the user name for the return Portal is, or?
    private $portalId = here I entered the name of the portal, So what's in the URL otherwise: amsel.dpwn.net/abholportal/gw/lp/portal/PORTALID/customer
    private $deliveryName =. << What is that??

    Thank you!

      • $deliveryName is the name of, the DHL for your company has determined. As soon as the return of the variant 3 (This necessary) has been activated you will receive an email with all deliveryName(s) has your company. BeispielfirmaSupertoll gets DE_beispiel_supertoll for example for the camp in Germany and CH_beispiel_supertoll for Switzerland.

        Understands me, the question above 2 Years was made, but since I had to find something longer I thought, It would be maybe good, to establish it here.

  4. The disable checking ssl is quite negligent:

    curl_setopt($soap_do, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($soap_do, CURLOPT_SSL_VERIFYHOST, false);

  5. The response I get the attribute to the BookLabelResponse “IDC” pass with.
    But I can't get.
    Can you tell me, I “$PDF = $soap->Body>children($NS['var3bl'])->BookLabelResponse->label;” must modify, to read this attribute?

  6. Thanks for this solution. Super!

    Save PDF would be:
    public function savePdf($pdf, $filename)
    {
    $FP = fopen($filename, “WB ”);
    fwrite($FP, $pdf);
    fclose($FP);
    }
    And then according to the call:
    $dhlRetoure->savePdf($pdf, “retoure.PDF”);

    PS: The parameter passed to getRetourePdf() must be UTF8 encoded.

  7. Hello Sebastian, Thanks for the code. Unfortunately, I get only a blank page and no PDF is generated. Return Variant 3 is active…. has possibly. something changed? Many greetings

  8. I get the following error message:

    SimpleXMLElement Object ( [Header] => SimpleXMLElement Object ( ) [Body] => SimpleXMLElement Object ( [@attributes] => Array ( [encodingStyle] => ) [Fault] => SimpleXMLElement object ( ) ) )
    Notice: Undefined index: var3bl in /www/htdocs/w00fface/buyrec-wp/dhl/retoure.php on line 114

  9. Good day. Now I'm also going to integrate that times. I'm working with cloud 9 and came following errors:

    Fatal error: Call to undefined function curl_init() in /home/ubuntu/workspace/action.php on line 104

    It has something with the curl_unit() and the missing php.ini to do? And does anyone know on straight away how you could integrate this with Cloud9?

    Also if it still works or whether something has changed from DHL there care, before I invest time there forever?

    Love greetings,
    Kloso

    • OK. Have now at least the bug fixed by using of:
      “sudo apt-get install php5-curl”.

      Now no error shows me, but as my shady just a white page. What could be the among other things?

  10. It is possible to collect the pdf without PHP, for example with google script or java script? Because I want to use it in my Chrome Extension without PHP

      • Would it be a big effort for you to build your code in Google Script, because I am not god in coding, and your code ist just working for me in php, but in Google script I am always getting a lot of errors. Thank you in advance :)

        • I tried the following approach in Google Script:
          function myFunction(){
          var xml = ‘……. ……”’

          var options =
          {
          “method” : “post”,
          “contentType” : “text/xml”,
          “payload” : xml
          };
          var result = UrlFetchApp.fetch(options);
          Logger.log(result)
          }

          But it is not enough apparently