I'm trying to get data from 17track.net, i mean when i tracking a number i got the result in the result box, but i can't display it in my file.
this is my code:
<?php
//RA190265307CN is the tracking number
$result = array();
$classname = "tb-stat";
$domdocument = new DOMDocument();
$domdocument->loadHTMLFile('http://www.17track.net/en/result/post.shtml?nums=RA190265307CN');
$a = new DOMXPath($domdocument);
$spans = $a->query("//*[contains(concat(' ', normalize-space(@class), ' '), ' $classname ')]");
for ($i = $spans->length - 1; $i > -1; $i--) {
$result[] = $spans->item($i)->firstChild->nodeValue;
}
echo "<pre>";
print_r($result); ?>
Thats the result of the array:
Array
(
[0] =>
)
Whats wrong with my code? I'm trying to get the "Status" of the tracking number.
The firstChild
will select a child <colgroup>
which is empty.
I think you want to replace
$result[] = $spans->item($i)->firstChild->nodeValue;
by
$result[] = $spans->item($i)->nodeValue;
This currently returns
Not Found(0) / Transporting(0) / Pick Up(0) / Delivered(0)
If you want to descend into this string even further you can take advantage of the fact that the status values "(0)"
are braced in <em>
tags. So, for example changing yout XPATH expression to
$query = "//*[contains(concat(' ', normalize-space(@class), ' '), ' $classname ')]//em";
will select the individual status and put them into the array result
. Using the index 0..3
you will be able to randomly access the status. If you want to have a numeric value you will still have to extract it from the string.