Search code examples
phpups

UPS tracking request doesn't return number of packages within shipment if there is more than one


I've got different shipments and some of them contain more then one package. I've created this request:

$data ="<?xml version=\"1.0\"?>
<AccessRequest xml:lang='en-US'>
        <AccessLicenseNumber>myLicenceNumber</AccessLicenseNumber>
        <UserId>myUserId</UserId>
        <Password>myPass</Password>
</AccessRequest>
<?xml version=\"1.0\"?>
<TrackRequest>
        <Request>
                <TransactionReference>
                        <CustomerContext>
                                <InternalKey>hello</InternalKey>
                        </CustomerContext>
                        <XpciVersion>1.0</XpciVersion>
                </TransactionReference>
                <RequestAction>Track</RequestAction>
        </Request>
<TrackingNumber>myTrackingNumber</TrackingNumber>
</TrackRequest>";
$ch = curl_init("https://www.ups.com/ups.app/xml/Track");
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch,CURLOPT_POST,1);
curl_setopt($ch,CURLOPT_TIMEOUT, 60);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch,CURLOPT_POSTFIELDS,$data);
$result=curl_exec ($ch);
$data = strstr($result, '<?');
$xml=simplexml_load_string($data);
print_r($xml);

It returns a lot of valuable information, but it doesn't return number of packages if there is more than one. How can I get this information, which is called "Multiple Packages"? Thank you.


Solution

  • I needed a quick solution so I decided to add this to my method:

    $tracking_url = 'http://wwwapps.ups.com/WebTracking/track?track=yes&trackNums='.$trackingNumber;
    
    $html = file_get_html($tracking_url);
    
    foreach($html->find('.module1 dd') as $e) {
        if(is_numeric($e->innertext)) {
            $number_of_packages = $e->innertext;
        }
    }
    

    This example uses simple_php_dom.php class.