获取DHL跟踪状态

时间:2017-06-07 09:41:08

标签: php tracking shipping dhl

请帮我弄清楚我应该使用哪些PHP API或PHP脚本从DHL the shipment statuses获得只有物流公司提供的DHL Tracking Codes来完成从电子商务中运送我们的订单网站。我的任务是创建一个PHP CronJob代码,该代码将检查并注册DHL跟踪运输的状态,以便在后端报告中使用它们。

我非常感谢任何可以帮助我找到正确方向的建议。

1 个答案:

答案 0 :(得分:0)

我仍然希望找到完成任务的正确方法。所以,除了解析DHL跟踪网页之外,我没有看到其他方式,因为只考虑了跟踪号码,它似乎不足以将它们用于某些API。 DHL API需要登录凭据,密钥等...但是,我当前的解析代码可能对寻找类似解决方案的人有用。只需包含您的跟踪代码并在您的本地主机甚至http://phpfiddle.org/上运行代码:

$tracking_array=Array('000000000000', '1111111111111'); // Tracking Codes

function create_track_url($track)
{
    $separator = '%2C+';
    $count = count($track);
    $url = '';
    if ($count < 2 && $count > 0){
       $url =  $track[0];
    }else if ($count >1){
        foreach ($track as $k => $v)
        {
          $sep = ($count-2);
            if ($k > $sep){
                $separator ='';
            }
          $url .=  $v.$separator;
        }
    }


   return $url; 
}
//load the html  
$dom = new DOMDocument(); 
$html = $dom->loadHTMLFile("https://nolp.dhl.de/nextt-online-public/en/search?piececode=".create_track_url($tracking_array));  

  //discard white space   
$dom->preserveWhiteSpace = false;  
  //the table by its tag name  


$xpath = new DOMXpath($dom);

$expression = './/h2[contains(@class, "panel-title")]';

$track_codes =array();
foreach ($xpath->evaluate($expression) as $div) {
  $track_codes[]= preg_replace( '/[^0-9]/', '', $div->nodeValue );
}

$tables = $dom->getElementsByTagName('table'); 
$table = array();
foreach($track_codes as $key => $val)

{
    //get all rows from the table  
$rows = $tables->item($key)->getElementsByTagName('tr');   
  // get each column by tag name  
$cols = $rows->item($key)->getElementsByTagName('th');   
$row_headers = NULL;
foreach ($cols as $node) {
    //print $node->nodeValue."\n";   
    $row_headers[] = $node->nodeValue;
}  

  //get all rows from the table  
$rows = $tables->item(0)->getElementsByTagName('tr');   
foreach ($rows as $row)   
{   
   // get each column by tag name  
    $cols = $row->getElementsByTagName('td');   
    $row = array();
    $i=0;
    foreach ($cols as $node) {
        # code...
        //print $node->nodeValue."\n";   
        if($row_headers==NULL)
            $row[] = $node->nodeValue;
        else
            $row[$row_headers[$i]] = $node->nodeValue;
        $i++;
    }   
    $table[$val][] = $row;
}
}   
print '<pre>';
print_r($table);