description = $description; $this->code = $code; } } /** * * VINModel * * A simple class to hold the description, code, and 0-n products * for each model entry inside the tag. * */ final class VINModel { public $description; public $code; public $products; public function __construct($description, $code) { $this->description = $description; $this->code = $code; $this->products = array(); } public function addProduct($description, $code) { $prod = new ModelProduct($description, $code); array_push($this->products, $prod); } public function products() { return $this->products; } } /** * * */ final class VINDecoder { /** * This is the user identity under which the request is authenticated * and authorized for use of the decoding facility. * */ private $username; /** * This is the user’s security token that is used along with the username * to authenticate and authorize the request for use of the decoding facility. * */ private $password; /** * This data represents the 17 character VIN to be decoded. * */ private $inputVIN; /** * This field tells the decoding system what to do in the case that the * decoded VIN information contains multiple vehicle models. If this value * is TRUE then the interface should automatically be sent back as the response. * This interface content (HTML, JavaScript, etc.) is returned in place of an * XML document. Possible values for this parameter include “TRUE” and “FALSE”. * * Note: The current implementation returns an HTTP Redirect to indicate * that the interface should be requested. * */ private $autoLaunchOnMultipleModels; /** * This field tells the decoding system what to do in the case that the * decoded VIN information contains decoding errors. If this value is * TRUE then the interface should automatically be sent back as the response. * This interface content (HTML, JavaScript, etc.) is returned in place of * an XML document. Possible values for this parameter include “TRUE” and “FALSE”. * * Note: The current implementation returns an HTTP Redirect to indicate * that the interface should be requested. * */ private $autoLaunchOnError; /** * This field indicates the language in which to return the decoding data. * Possible values for this parameter include “en_ca” and “fr_ca”. * Optionally, the value for this parameter or even the parameter itself * may be left out of the request. If this is the case then the decoding * data will be returned in the user’s default language. * * Note: Any decoding errors returned will be in the user’s default language * regardless of the value specified on this parameter. * */ private $language; /** * * */ private $decoded; /** * * */ private $XMLResponse; /** * * */ private $systemError; /** * * */ private $systemErrorCode; /** * * */ private $systemErrorMessage; /** * * */ private $decodeError; /** * * */ private $decodeErrorCode; /** * * */ private $decodeErrorMessage; /** * * */ private $vin; /** * * */ private $yearcode; /** * * */ private $yearvalue; /** * * */ private $sequentialnumber; /** * * */ private $vehicletype; /** * * */ private $marketingmake; /** * * */ private $models; /** * * */ public function __construct($username, $password, $inputVIN, $autoModels = false, $autoError = false, $language = 'en_ca') { $this->username = $username; $this->password = $password; $this->inputVIN = $inputVIN; $this->setAutoLaunchOnMultipleModels($autoModels); $this->setAutoLaunchOnError($autoError); $this->setLanguage($language); $this->decoded = false; $this->XMLResponse = null; $this->systemError = false; $this->systemErrorCode = null; $this->systemErrorMessage = null; $this->decodeError = false; $this->decodeErrorCode = null; $this->decodeErrorMessage = null; $this->vin = null; $this->yearcode = null; $this->yearvalue = null; $this->sequentialnumber = null; $this->vehicletype = null; $this->marketingtype = null; $this->models = null; } /** * * */ public function __destruct() { } /** * * */ public function __toString() { return htmlentities($this->getRawXMLResponse()); } /** * Access methods for VINDecoder base attributes * */ public function isDecoded() { return $this->decoded; } public function getRawXMLResponse() { return $this->XMLResponse; } /** * Access methods for error codes, and messages set during decoding * */ public function hasSystemError() { return $this->systemError; } public function getSystemErrorCode() { return $this->systemErrorCode; } public function getSystemErrorMessage() { return $this->systemErrorMessage; } public function hasDecodeError() { return $this->decodeError; } public function getDecodeErrorCode() { return $this->decodeErrorCode; } public function getDecodeErrorMessage() { return $this->decodeErrorMessage; } /** * Access methods for decoded VIN information * */ public function vin() { return $this->vin; } public function yearcode() { return $this->yearcode; } public function yearvalue() { return $this->yearvalue; } public function sequentialnumber() { return $this->sequentialnumber; } public function vehicletype() { return $this->vehicletype; } public function marketingmake() { return $this->marketingmake; } public function models() { return $this->models; } /** * Set the internal autoLaunchOnMultipleModels property. * The values are not boolean, but converted to string 'TRUE' or 'FALSE' * */ public function setAutoLaunchOnMultipleModels($flag) { if ($flag) { $this->autoLaunchOnMultipleModels = 'TRUE'; } else { $this->autoLaunchOnMultipleModels = 'FALSE'; } } /** * Set the internal autoLaunchOnError property. * The values are not boolean, but converted to string 'TRUE' or 'FALSE' * */ public function setAutoLaunchOnError($flag) { if ($flag) { $this->autoLaunchOnError = 'TRUE'; } else { $this->autoLaunchOnError = 'FALSE'; } } /** * Set the internal language property. * Default to en_ca. * */ public function setLanguage($language) { if ($language != 'en_ca' && $language != 'en_fr') { $language = 'en_ca'; } $this->language = $language; } /** * * */ private function parse() { if (isset($this->XMLResponse) && is_string($this->XMLResponse) && strlen($this->XMLResponse) > 0) { $xml = new SimpleXMLElement($this->XMLResponse); /* An error will be returned as : Failed to authenticate 2 The SimpleXMLElement name will become "sysError", having 2 attributes "message, and code" */ if ($xml->getName() === 'sysError') { $this->systemError = true; $this->systemErrorCode = $xml->code; $this->systemErrorMessage = $xml->message; $this->decoded = false; return false; } else if ($xml->getName() === 'vininfo') { /* A response will be formed with any decodeerror sections included Invalid VIN 3 ... ... In the even that there are no errors, and the VIN has been properly decoded : ... ... */ if (count($xml->decodeerror->children()) > 0) { $this->decodeError = true; $this->decodeErrorCode = $xml->decodeerror->code; $this->decodeErrorMessage = $xml->decodeerror->message; $this->decoded = false; return false; } else { $this->vin = $xml->vin; $this->yearcode = $xml->yearcode; $this->yearvalue = $xml->yearvalue; $this->sequentialnumber = $xml->sequentialnumber; $this->vehicletype = $xml->vehicletype; $this->marketingmake = $xml->marketingmake; // loop through all models $this->models = array(); foreach($xml->models->children() as $model) { $vinModel = new VINModel($model->description, $model->code); foreach ($model->products->children() as $product) { $vinModel->addProduct($product->description, $product->code); } array_push($this->models, $vinModel); } $this->decoded = true; return true; } } } return false; } /** * * */ public function decode() { $this->decoded = false; $this->XMLResponse = null; $this->systemError = false; $this->systemErrorCode = null; $this->systemErrorMessage = null; $this->decodeError = false; $this->decodeErrorCode = null; $this->decodeErrorMessage = null; $this->vin = null; $this->yearcode = null; $this->yearvalue = null; $this->sequentialnumber = null; $this->vehicletype = null; $this->marketingtype = null; $this->models = null; $req =& new HTTP_Request(IBC_VIN_WEBSERVICE_URL); $req->setMethod(HTTP_REQUEST_METHOD_POST); $req->clearPostData(); $req->addPostData("username", $this->username); $req->addPostData("password", $this->password); $req->addPostData("inputVIN", $this->inputVIN); $req->addPostData("autoLaunchOnMultipleModels", $this->autoLaunchOnMultipleModels); $req->addPostData("autoLaunchOnError", $this->autoLaunchOnError); $req->addPostData("language", $this->language); if (!PEAR::isError($req->sendRequest())) { switch ($req->getResponseCode()) { case 200 : { $this->XMLResponse = $req->getResponseBody(); return $this->parse(); break; } case 302 : { // redirect sent for external launch } default : break; } } return false; } } ?>