"); $track->addAttribute('USERID', env("usps_user_id")); $track->addChild('Revision', '1'); $track->addChild('ClientIp', $_SERVER['REMOTE_ADDR']); $track->addChild('SourceId', env("usps_source_id", "selfhosted-opensource-default-value.netsyms.net")); $pack = $track->addChild('TrackID'); $pack->addAttribute('ID', $barcode->getSanitized()); $url = 'https://secure.shippingapis.com/ShippingApi.dll?API=TrackV2&XML=' . $track->asXML(); $xml = simplexml_load_file($url); if ($xml->getName() == "Error") { if (!empty($xml->Description)) { throw new TrackingException("The USPS tracking system is having problems: \"" . trim($xml->Description) . "\""); } throw new TrackingException("The USPS tracking system is having problems. Try again later."); } if (!empty($xml->TrackInfo)) { $trackinfo = $xml->TrackInfo; } if (!empty($xml->TrackInfo->Error)) { throw new TrackingException((string) $xml->TrackInfo->Error->Description); } } catch (TrackingException $ex) { throw $ex; } catch (Exception $ex) { throw new TrackingException("There was a server problem. This code cannot be tracked right now."); } $info = new TrackingInfo(); try { $info->setCode($trackinfo->attributes()["ID"]); } catch (Exception $ex) { throw new TrackingException("The USPS tracking system returned an invalid response. Try again later."); } $info->setCarrier("usps"); $info->setService(new Service((string) $trackinfo->ClassofMailCode, (string) $trackinfo->Class)); // $info->setCarrierLogo(($_SERVER['SERVER_PORT'] == 443 ? "https://" : "http://") // . $_SERVER['HTTP_HOST'] . env("urlbase", "/") // . "resources/logistics.tracking/logos/usps.png"); $info->setCarrierAttributionText("Information provided by www.usps.com."); $current_status = new TrackingEntry( TrackingStatus::USPSEventCodeToStatus($trackinfo->TrackSummary->EventCode), ($trackinfo->StatusSummary ?? "Unknown") . (TrackingStatus::USPSEventCodeToStatus($trackinfo->TrackSummary->EventCode) == TrackingStatus::TRACKING_STATUS_UNKNOWN ? " " . $trackinfo->TrackSummary->EventCode : ""), $trackinfo->TrackSummary["EventDate"] . " " . $trackinfo->TrackSummary["EventTime"], null, TrackingStatus::isUSPSEventCodeContainerScan($trackinfo->TrackSummary->EventCode) ); $current_location = new Location(); $current_location->city = (string) $trackinfo->TrackSummary->EventCity ?? ""; $current_location->state = (string) $trackinfo->TrackSummary->EventState ?? ""; $current_location->zip = (string) $trackinfo->TrackSummary->EventZIPCode ?? ""; $current_location->country = (string) $trackinfo->TrackSummary->EventCountry ?? ""; $current_status->setLocation($current_location); $info->setCurrentStatus($current_status); // USPS doesn't put the latest entry in the history $info->appendHistoryEntry($current_status); $from = new Location(); $from->city = (string) $trackinfo->OriginCity ?? ""; $from->state = (string) $trackinfo->OriginState ?? ""; $from->zip = (string) $trackinfo->OriginZip ?? ""; $from->country = (string) $trackinfo->OriginCountryCode ?? ""; $info->setFrom($from); $to = new Location(); $to->city = (string) $trackinfo->DestinationCity ?? ""; $to->state = (string) $trackinfo->DestinationState ?? ""; $to->zip = (string) $trackinfo->DestinationZip ?? ""; $to->country = (string) $trackinfo->DestinationCountryCode ?? ""; $info->setTo($to); foreach ($trackinfo->TrackDetail as $history) { $location = new Location(); $location->city = (string) $history->EventCity ?? ""; $location->state = (string) $history->EventState ?? ""; $location->zip = (string) $history->EventZIPCode ?? ""; $location->country = (string) $history->EventCountry ?? ""; $info->appendHistoryEntry(new TrackingEntry( TrackingStatus::USPSEventCodeToStatus((string) $history->EventCode), ((string) $history->Event), $history->EventDate . " " . $history->EventTime, $location, TrackingStatus::isUSPSEventCodeContainerScan((string) $history->EventCode))); } return $info; } }