Created
March 26, 2015 15:13
-
-
Save eni23/06c7ada2e19918793227 to your computer and use it in GitHub Desktop.
Quick and Dirty class to Query Colissimo-API with Support for mulitple languages
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Quick and Dirty class to Query Colissimo-API | |
* Support for mulitple languages | |
* | |
* @author Cyrill von Wattenwyl < [email protected] > | |
* @deps DOM, Allow http for file_get_contents in php.ini | |
* | |
**/ | |
class colissimo { | |
// url to query | |
protected $baseurl="http://www.colissimo.fr/portail_colissimo/suivre.do"; | |
// valid languages | |
protected $valid_languages=array("fr_FR", "de_DE", "en_GB", "nl_NL", "it_IT", "es_ES"); | |
// default language | |
public $lang="en_GB"; | |
/** | |
* Track a Package | |
* | |
* @param id{string} Colissimo-ID | |
* @return array Array with Tracking-Informations | |
**/ | |
function query( $id ){ | |
// load data and parse to dom-object | |
$result = array(); | |
$url = "{$this->baseurl}?language={$this->lang}&colispart={$id}"; | |
$html = file_get_contents($url); | |
$dom = new DOMDocument($html); | |
@$dom -> loadHTML($html); | |
$finder = new DomXPath($dom); | |
// check for error | |
$err = $finder->query("//*[contains(@class, 'error')]"); | |
if ($err->length > 0){ | |
$val = trim( $err->item(0)->textContent ); | |
array_push($result,$val); | |
return $result; | |
} | |
// parse messages | |
$res = $finder->query("//*[contains(@class, 'dataArray')]"); | |
$msgs = $res->item(0)->getElementsByTagName('tr'); | |
for ($i=1; $i<$msgs->length; $i++){ | |
$item = $msgs->item($i)->getElementsByTagName('td'); | |
$date = str_replace("/",".", trim ( $item->item(0)->textContent ) ); | |
$msg = trim ( $item->item(1)->textContent ); | |
$loc = trim ( $item->item(2)->textContent ); | |
$arr = array( "date" => $date, "message" => $msg, "location" => $loc ); | |
array_unshift($result,$arr); | |
} | |
// return them | |
return $result; | |
} | |
/** | |
* Set Language of Messages | |
* | |
* @param lang{string} Language | |
* @return bool True if Successfull, false if not | |
**/ | |
function setlocale($lang){ | |
// no language string if french | |
if ($lang=="fr_FR"){ | |
$this->lang=''; | |
return true; | |
} | |
if ( array_search($lang, $this->valid_languages) != false ){ | |
$this->lang=$lang; | |
return true; | |
} | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment