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

StackOverflow https://stackoverflow.com/questions/21734122

  •  10-10-2022
  •  | 
  •  

Pergunta

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.

Foi útil?

Solução 2

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.

Outras dicas

You could try setting up webhooks with something like EasyPost? You can see what package variables the USPS requires here - https://www.easypost.com/docs/api#parcels

Otherwise, for more help can you show me what this call is returning?

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top