请帮我弄清楚我应该使用什么 PHP API 或 PHP 脚本从 DHL 获取the shipment statuses
只有DHL Tracking Codes
物流公司提供,该物流公司可以从电子商务网站完成我们的订单运输。我的任务是创建一个 PHP CronJob 代码,该代码将检查和注册 DHL 跟踪运输的状态,以便在后端报告中使用它们。
我非常感谢任何可以帮助我找到正确方向的建议。
我仍在寻找正确的方法来完成我的任务。所以,到目前为止,除了解析 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);