DHL Tracking Api and PHP

前端 未结 4 1610
北恋
北恋 2021-02-09 06:03

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 exa

4条回答
  •  难免孤独
    2021-02-09 06:17

    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  = '';
       $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.

提交回复
热议问题