我仍在尋找找到正確的方式來完成我的任務。所以,我認爲除了解析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);
https://github.com/jklz/DHL-API-Tracking-PHP –
謝謝你的建議親愛的@AhmedGinani:我分析了這個腳本。看起來PHP代碼要求DHL安全號碼與DHL跟蹤代碼有所不同。 Airbill Number - 10個字符的長度編號; 追蹤代碼 - 12個字符的長度數字; –