0

Please help me to figure out what PHP API or PHP script should I use to get from DHL the shipment statuses having only available DHL Tracking Codes provided by the logistic company which fulfill shipping of our orders from e-commerce website. My Task is to create a PHP CronJob code which would check and register the Status of DHL Tracking Shipping for using them in back-end reports.

I would much appreciate any suggestion which may help me to find the right direction.

Daniel Widdis
  • 8,424
  • 13
  • 41
  • 63
Sergiu Costas
  • 530
  • 4
  • 8
  • 26
  • https://github.com/jklz/DHL-API-Tracking-PHP – Ahmed Ginani Jun 07 '17 at 09:53
  • Thank you for suggestion dear @AhmedGinani: I have analysed that script. It seems that PHP Code requires DHL Airbil Number which is something different than DHL Tracking Code. Airbill Number - 10 characters length number; Tracking code - 12 characters length number; – Sergiu Costas Jun 07 '17 at 11:36

1 Answers1

0

I am still looking to find the right way to achieve my task. So, far I do not see other way than Parsing DHL Tracking webpage considering having only Tracking Number available which it seems to be insufficient for using them for some API. DHL API requires Login credentials, secret keys and so on... However, my current parsing code might be useful for someone who looks for similar solution. Just include your Tracking Codes and run the code in your localhost or even on 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);
Sergiu Costas
  • 530
  • 4
  • 8
  • 26
  • Have you find the solution for getting the tracking details from DHL? It will be really helpful if you can share that – Sandeep Jul 22 '19 at 10:41
  • I used example I have shared above, customizing it by my needs. So I have created a script which run couple of times per day: firstly, it checked all unfulfilled shipments yet, secondly updating the last changes changing and then if customers do not getting within 24 hours their orders, then: 1) alerting Sales department; automatically sending emails to customers informing about picking up their shipments. Yep, it helped much to minimize costly returns from DHL. – Sergiu Costas Aug 09 '19 at 08:13