Tracking DHL Api and PHP

I am currently working on a project where I need to receive the status of a package (sent from DHL). I read about the DHL API, which returns XML, but for some reason there are no good examples. I found some code snippets, but I don’t know where to register for the API keys.

Does anyone have any links or examples for me?

Regards, Lukas

+7
source share
4 answers

There is also this PHP client that you can use to use the XML XML API. It can handle all the various services provided by DHL.

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

This client is independent and platform independent, and should be fairly easy to integrate with your own code. You can check the examples folder, for example, on how to use it.

+5
source

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

It is used to connect to DHL using XML-PI to track shipments using Bill Air Bill. it can process one tracking number or as much as you submit it (was tested with 250 and another, and then spend a little time on launch, there were no problems). automatically takes and breaks the array of tracking numbers into pieces, and then sends the request to DHL so as not to miss the maximum number that can be tracked for the request, and returns the results as an array.

+1
source

There is a good blog about it. Unfortunately, in German, but the code that displays there should still make sense to you.

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

Excerpts:

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 returns 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 ) 

(Actually there is WAY more data.)

Hope this helps you in some way.

0
source

Quick and dirty without any third-party libraries and using the 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"; } 
0
source

All Articles