phptrackingdhl

get DHL tracking statuses


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.


Solution

  • 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);