9

I'm currently working on a project, where i have to get the status of a packet (sent with DHL). I read about the DHL API, which return an XML, but somehow there are no good examples out there. I have found some code snippets, but i have no clue where to register for API Key's.

Have anyone some links or examples for me?

Best regards, Lukas

Lukas
  • 1,346
  • 7
  • 24
  • 49
  • Did you get anywhere with this as I have a similar issue: http://stackoverflow.com/questions/16860991/dhl-trackshipmentrequest-wdsl-php-soap – The Humble Rat Jun 03 '13 at 16:02
  • No, not really. I stopped the project as it was to work intensive as there are no good / real API's around there. – Lukas Jun 04 '13 at 10:19

4 Answers4

8

There is also this PHP client that can be used to consume the DHL XML API. It can handle all the different services exposed by DHL.

https://github.com/alfallouji/DHL-API

This client does not rely or depend on any framework and it should be fairly easy to integrate with your own code. You can check the samples folder for example on how to use it.

Bashar
  • 96
  • 1
  • 2
2

https://github.com/jklz/DHL-API-Tracking-PHP

It is used to connect into DHL using the XML-PI to track shipments using the Air Way Bill. it can handle a single tracking number or as many as you feed into it (has been tested with 250 and other then taking a little time to run had no problems). automatically takes and breaks the array of tracking numbers into chunks and then sends the request to DHL making sure not to pass the max number that can be tracked per request then returns the results as a array.

Garry
  • 4,996
  • 5
  • 32
  • 44
  • Hi, thank's for your answer. I already found that one, but it's not working properly as it contains bugs and it needs to authenticate with a username and password. Do you know where to get these? – Lukas Feb 15 '13 at 20:46
1

Quick and dirty without any third party lib and using official API:

<?php
$mode        = 'sandbox'; // sandbox or production
$username    = ''; // dhl developer account name, not email
$password    = ''; // dhl developer account pass
$appname     = 'zt12345'; // sandbox app
$apppass     = 'geheim'; // sandbox app
$endpoint    = 'https://cig.dhl.de/services/' . $mode . '/rest/sendungsverfolgung';
$payload     = simplexml_load_string( '<?xml version="1.0" encoding="UTF-8" standalone="no"?><data appname="' . $appname . '" language-code="de" password="' . $apppass . '" piece-code="" request="d-get-piece-detail"/>' );
$shipmentids = array(
    '00340434161094015902' // in sandbox only special numbers are allowed
);


$opts = array(
    'http' => array(
        'method' => "GET",
        'header' => "Authorization: Basic " . base64_encode( "$username:$password" )
    )
);

$context = stream_context_create( $opts );


foreach ( $shipmentids as $shipmentid ) {
    $payload->attributes()->{'piece-code'} = $shipmentid;
    $response                              = file_get_contents( $endpoint . '?' . http_build_query( array( 'xml' => $payload->saveXML() ) ), false, $context );
    $responseXml                           = simplexml_load_string( $response );
    $status                                = null;

    // get last state
    foreach ( $responseXml->data->data->data as $event ) {
        $status = $event->attributes()->{'event-short-status'};
    }

    echo "Shipment " . $shipmentid . " is in state: " . $status . "\n";
}
Daniel Walter
  • 785
  • 5
  • 12
  • Is there a possibilty to add several tracking numbers to the $shipmentids array? I tried with another sandbox tracking number (00340434161094022115) however it does not return a result at all. – Chris May 02 '21 at 08:26
  • 1
    Just talked to DHL Developer: It is not possible to use several tracking numbers in the sandbox but works in production. – Chris May 03 '21 at 08:31
0

There is a nice blog about this. It is unfortunately in German, but the code that is displayed there should still make sense to you.

Source: https://blog.simlau.net/dhl-tracking-api-php.html

Excerpt:

function dhl_tracking($trackingnumber)
{
   $data  = '<?xml version="1.0" encoding="ISO-8859-1" ?>';
   $data .= '<data appname="nol-public" password="anfang" request="get-status-for-public-user" language-code="de">';
   $data .= '  <data piece-code="'.$trackingnumber.'"></data>';
   $data .= '</data>';

   // URL bauen und File hohlen
   $xml = simplexml_load_file(sprintf(
      'http://nolp.dhl.de/nextt-online-public/direct/nexttjlibpublicservlet?xml=%s', $data
   ));

   // FALSE, wenn Syntax oder HTTP Error
   if ($xml === false) return false;

   // Wandelt das SimpleXML Objekt in ein Array um
   foreach ($xml->data->data->attributes() as $key => $value) {
      $return[$key] = (string) $value;
   }
   return $return;
}

// Aufruf der Funktion
print_r(dhl_tracking($tracking_number));

This function will give back an array that will contain some tracking information:

Array
(
    [status] => Die Sendung wurde erfolgreich zugestellt.
    [recipient-id-text] => Nachbar
    [product-name] => DHL PAKET
    [pan-recipient-name] => SIMON LAUGER
)

(In fact, there is WAY more data in there.)

I hope this will help you in some way.

Kat Seiko
  • 125
  • 9
  • Change the language code to "en" (or whatever suits you, provided DHL supports it) and your code returns data in English. I have not yet found a list of provided languages. – Kat Seiko Nov 18 '16 at 09:59