-->

DHL Tracking Api and PHP

2020-06-04 02:23发布

问题:

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

回答1:

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.



回答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.



回答3:

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";
}


回答4:

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.



标签: php tracking