<?php
/*
@release date: 23.04.2010
@original-author Petter Kjelkenes from Norway
@support kjelkenes [AT] gmail [DOT] com
@website http://pkj.no , www.pkj.no
@version 0.2
@license LGPL
@requries > PHP 5.x
Norway specific (Posten) delivery API wrapper.
Wrapper for getting the trace for a specific delivery.
You will need the trace-code/ Shippingnumber to get information back!
Features:
- Fully funcitonal against the XML api.
- Very efficient.
- Realtime tracking of package and the last steps the package was thru.
- Supports multiple packages and multiple everything, therefor you'd want to use some foreach() in your query, see example!
EXAMPLE:
// -----------
try{
// Tracking number / Shipping number / Delivery number in the first argument!
$track = new PostenTrack('SMXXXXXXXXNO'); // Replace with the delivery you'd want to track.
// There can be multiple Consignments in a delivery.... So Each Consignment.
foreach($track->Consignments() as $consignment){
echo "<h1>The package(s) total weight: ",$consignment->getWeight(), " and volume is ",$consignment->getVolume(),"</h1> <br />";
// There can be multiple Packages in a consignment.... So Each Package.
foreach($consignment->Packages() as $package){
echo "<h2>Package using the product. ",$package->getProductName(),"...
Size: ",$package->getWidth(),"x",$package->getLength(),"x",$package->getHeight()," ... Volume: ",$package->getVolume(),"</h2> ";
echo "<h3>Last events:</h3>";
// There can be multiple Events in a Package ... So Eeach Event
// This is proberly what you'd want to display to your customers.
foreach($package->Events() as $event){
echo $event->getCity(),"- ",$event->getPostalCode()," ... ",$event->getDate()," ",$event->getTime()," ..... ",$event->getDescription()," <br />";
}
}
echo "<br /><br />";
}
}catch(Exception $e){
echo "ERROR: ", $e->getMessage();
}
// -----
END EXAMPLE
*/
class PostenTrack{
private $xml, $number;
public function __construct($number){
$this->number = $number;
$url = 'http://sporing.posten.no/sporing.xml?q='.$this->number;
// Will have to set a fake useragent, to be able to fetch stuff from posten.
$opts = array(
'http'=>array(
'method'=>"GET",
'header'=>"Accept: application/xml\r\n" .
"Accept-language: en\r\n" .
"Cookie: foo=bar\r\n" ,
'user_agent'=> 'Firefox (WindowsXP) - Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6'
)
);
// Create fake headers.
$context = stream_context_create($opts);
// Get content
$content = @file_get_contents($url,false,$context);
$this->xml = @simplexml_load_string($content);
$headerdata = explode(' ',$http_response_header[0]);
$headercode = (int)$headerdata[1];
if (strstr($content,"No consignment found"))throw new Exception("The shippingnumber $number was not found.");
switch($headercode){
case 400:
case 500:
default:
throw new Exception("Service not available at the moment. This is error on the API side (posten).",1);
break;
case 200:
break;
}
}
public function Consignments(){
$ret=array();
foreach($this->xml->Consignment as $consignment){
$ret[] = new PostenConsignment($consignment);
}
return $ret;
}
}
class PostenConsignment{
private $obj;
public function __construct($obj){
$this->obj = $obj;
}
public function Packages(){
$ret=array();
foreach($this->obj->PackageSet->Package as $package){
$ret[] = new PostenPackage($package);
}
return $ret;
}
public function getWeight(){
return $this->obj->TotalWeight;
}
public function getVolume(){
return $this->obj->TotalVolume;
}
}
class PostenPackage{
private $obj;
public function __construct($obj){
$this->obj = $obj;
}
public function getProductName(){
return $this->obj->ProductName;
}
public function getProductCode(){
return $this->obj->ProductCode;
}
public function getBrand(){
return $this->obj->Brand;
}
public function getWeight(){
return $this->obj->Weight;
}
public function getLength(){
return $this->obj->Length;
}
public function getWidth(){
return $this->obj->Width;
}
public function getHeight(){
return $this->obj->Height;
}
public function getVolume(){
return $this->obj->Volume;
}
public function Events(){
$ret=array();
foreach($this->obj->EventSet->Event as $event){
$ret[] = new PostenEvent($event);
}
return $ret;
}
}
class PostenEvent{
private $obj;
public function __construct($obj){
$this->obj = $obj;
}
public function getDescription(){
return $this->obj->Description;
}
public function getStatus(){
return $this->obj->Status;
}
public function getRecipientSignature(){
return $this->obj->RecipientSignature->Name;
}
public function getID(){
return $this->obj->UnitId;
}
public function getPostalCode(){
return $this->obj->PostalCode;
}
public function getCity(){
return $this->obj->City;
}
public function getTimestamp(){
return $this->obj->OccuredAtIsoDateTime;
}
public function getDate(){
return $this->obj->OccuredAtDisplayDate;
}
public function getTime(){
return $this->obj->OccuredAtDisplayTime;
}
}
?>