Scripts
Kenteken API
Ik heb deze API gemaakt, omdat ik op een manier die netjes en makkelijk is gegevens over een kenteken kan opzoeken en makkelijk te gebruiken en te implementeren in PHP scripts. Je gebruikt het zo: Nu krijg ik bijvoorbeeld het aantal zitplaatsen, maar je kan ook informatie vinden over de APK, Merk, brandstof soort en nog veel meer!
KentekenAPI.php
<?php
Class KentekenAPI {
protected $api = 'http://rdw.almere.pilod.nl/kentekens/%s';
protected $properties;
public function __construct($license = null) {
$url = sprintf( $this->api, self::validate($license) );
$data = $this->sendRequest($url);
$this->properties = json_decode($data, true);
$this->properties = self::convertToObject($this->properties);
}
public function __get($key) {
if( isset($this->properties->resource) && array_key_exists($key, $this->properties->resource) ) {
return $this->properties->resource->$key;
}
return null;
}
public function all() {
return $this->properties->resource;
}
protected function sendRequest($url) {
$curl = curl_init();
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_URL, $url);
return curl_exec($curl);
}
static protected function convertToObject($array) {
$object = new stdClass();
foreach($array as $key => $value) {
if(is_array($value)) {
$value = self::convertToObject($value);
}
$object->$key = $value;
}
return $object;
}
public function error() {
return (isset($this->properties->error->message)) ? $this->properties->error->message : false;
}
static protected function validate($string = null) {
return preg_replace('/[^a-zA-Z0-9]+/', '', $string);
}
}
Reacties
0