DHL 追踪 Api 和 PHP
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/14902983/
Warning: these are provided under cc-by-sa 4.0 license.  You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
DHL Tracking Api and PHP
提问by Lukas
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.
我目前正在做一个项目,在那里我必须获取数据包的状态(通过 DHL 发送)。我读过 DHL API,它返回一个 XML,但不知何故那里没有好的例子。我找到了一些代码片段,但我不知道在哪里注册 API 密钥。
Have anyone some links or examples for me?
有没有人给我一些链接或例子?
Best regards, Lukas
最好的问候,卢卡斯
采纳答案by Bashar
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.
还有这个 PHP 客户端可用于使用 DHL XML API。它可以处理 DHL 公开的所有不同服务。
https://github.com/alfallouji/DHL-API
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.
这个客户端不依赖或依赖任何框架,它应该很容易与您自己的代码集成。例如,您可以检查示例文件夹以了解如何使用它。
回答by Garry
https://github.com/jklz/DHL-API-Tracking-PHP
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.
它用于使用 XML-PI 连接到 DHL,以使用空运提单跟踪货件。它可以处理单个跟踪号,也可以处理您输入的跟踪号(已用 250 进行了测试,其他然后花一点时间运行没有问题)。自动将跟踪号码数组分成块,然后将请求发送到 DHL,确保不传递每个请求可以跟踪的最大数量,然后将结果作为数组返回。
回答by Daniel Walter
Quick and dirty without any third party lib and using official API:
没有任何第三方库并使用官方 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";
}
回答by Kat Seiko
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
来源: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.
我希望这会以某种方式帮助你。

