dl/includes/ApiRequestDummy.php
2020-06-17 20:49:28 +02:00

96 lines
No EOL
7.8 KiB
PHP

<?php
class ApiRequest {
private static $baseURL = 'https://www.delijn.be/rise-api-core/';
private $action;
private $url;
private $http_response_header;
public function __toString () {
return 'ApiRequest[url=' . $this->url . ']';
}
public function __construct ($action, $method='GET') {
if (!is_string($action)) {
throw new InvalidArgumentException('$action must be a string');
}
$this->action = $action;
$this->url = Static::$baseURL.$action;
if (preg_match('~.*//.*//~',$this->url) === 1) {
throw new InvalidArgumentException('Resulting URL ('.$this->url.') contains a double slash');
}
if (!is_string($method)) {
throw new InvalidArgumentException('$method must be a string');
}
switch ($method) {
case 'GET': case 'POST': case 'PUT': case 'DELETE': break;
throw new InvalidArgumentException('$method must be one of the following: "GET", "POST", "PUT", "DELETE"');
}
$this->method = $method;
}
public function exec ($dataToSend=null) {
$opts = array('http' =>
array(
'method' => $this->method
)
);
if (!is_null($dataToSend)) {
if (is_array($dataToSend)) {
$opts['http']['content'] = http_build_query($dataToSend);
} else {
throw new InvalidArgumentException('$dataToSend must be either NULL or an array');
}
}
$context = stream_context_create($opts);
$json = '{"bestemmingen":null,"coordinaat":null,"entiteitNummer":0,"halteNummer":0,"huidigeDag":"maandag","huidigeTijd":"1:49","id":0,"interneLijnnummers":null,"laatstGebruikt":null,"lijnen":[{"bestemming":"KNOKKE STATION","entiteitNummer":5,"gemeentes":null,"haltes":null,"id":0,"kleurAchterGrond":"#BB0022","kleurAchterGrondRand":"#BB0022","kleurVoorGrond":"#FFFFFF","kleurVoorGrondRand":"#000000","lijnGeldigVan":null,"lijnNummer":500,"lijnNummerPubliek":"0 ","lijnRichting":null,"lijnType":"tram","lijnTypeLink":"TRAMLIJN","omschrijving":null,"omschrijvingHighlighted":null,"richtingCode":0,"richtingCodeAndereRichting":0,"ritNummer":25500,"ritOrder":1603,"vertrekCalendar":1434338580000,"vertrekTijd":"5:23"},{"bestemming":"KNOKKE STATION","entiteitNummer":5,"gemeentes":null,"haltes":null,"id":0,"kleurAchterGrond":"#BB0022","kleurAchterGrondRand":"#BB0022","kleurVoorGrond":"#FFFFFF","kleurVoorGrondRand":"#000000","lijnGeldigVan":null,"lijnNummer":500,"lijnNummerPubliek":"0 ","lijnRichting":null,"lijnType":"tram","lijnTypeLink":"TRAMLIJN","omschrijving":null,"omschrijvingHighlighted":null,"richtingCode":0,"richtingCodeAndereRichting":0,"ritNummer":45500,"ritOrder":1601,"vertrekCalendar":1434341580000,"vertrekTijd":"6:13"},{"bestemming":"KNOKKE STATION","entiteitNummer":5,"gemeentes":null,"haltes":null,"id":0,"kleurAchterGrond":"#BB0022","kleurAchterGrondRand":"#BB0022","kleurVoorGrond":"#FFFFFF","kleurVoorGrondRand":"#000000","lijnGeldigVan":null,"lijnNummer":500,"lijnNummerPubliek":"0 ","lijnRichting":null,"lijnType":"tram","lijnTypeLink":"TRAMLIJN","omschrijving":null,"omschrijvingHighlighted":null,"richtingCode":0,"richtingCodeAndereRichting":0,"ritNummer":85500,"ritOrder":1608,"vertrekCalendar":1434345180000,"vertrekTijd":"7:13"},{"bestemming":"KNOKKE STATION","entiteitNummer":5,"gemeentes":null,"haltes":null,"id":0,"kleurAchterGrond":"#BB0022","kleurAchterGrondRand":"#BB0022","kleurVoorGrond":"#FFFFFF","kleurVoorGrondRand":"#000000","lijnGeldigVan":null,"lijnNummer":500,"lijnNummerPubliek":"0 ","lijnRichting":null,"lijnType":"tram","lijnTypeLink":"TRAMLIJN","omschrijving":null,"omschrijvingHighlighted":null,"richtingCode":0,"richtingCodeAndereRichting":0,"ritNummer":105500,"ritOrder":1609,"vertrekCalendar":1434346980000,"vertrekTijd":"7:43"},{"bestemming":"KNOKKE STATION","entiteitNummer":5,"gemeentes":null,"haltes":null,"id":0,"kleurAchterGrond":"#BB0022","kleurAchterGrondRand":"#BB0022","kleurVoorGrond":"#FFFFFF","kleurVoorGrondRand":"#000000","lijnGeldigVan":null,"lijnNummer":500,"lijnNummerPubliek":"0 ","lijnRichting":null,"lijnType":"tram","lijnTypeLink":"TRAMLIJN","omschrijving":null,"omschrijvingHighlighted":null,"richtingCode":0,"richtingCodeAndereRichting":0,"ritNummer":125500,"ritOrder":1617,"vertrekCalendar":1434347580000,"vertrekTijd":"7:53"},{"bestemming":"KNOKKE STATION","entiteitNummer":5,"gemeentes":null,"haltes":null,"id":0,"kleurAchterGrond":"#BB0022","kleurAchterGrondRand":"#BB0022","kleurVoorGrond":"#FFFFFF","kleurVoorGrondRand":"#000000","lijnGeldigVan":null,"lijnNummer":500,"lijnNummerPubliek":"0 ","lijnRichting":null,"lijnType":"tram","lijnTypeLink":"TRAMLIJN","omschrijving":null,"omschrijvingHighlighted":null,"richtingCode":0,"richtingCodeAndereRichting":0,"ritNummer":145500,"ritOrder":1612,"vertrekCalendar":1434348780000,"vertrekTijd":"8:13"},{"bestemming":"KNOKKE STATION","entiteitNummer":5,"gemeentes":null,"haltes":null,"id":0,"kleurAchterGrond":"#BB0022","kleurAchterGrondRand":"#BB0022","kleurVoorGrond":"#FFFFFF","kleurVoorGrondRand":"#000000","lijnGeldigVan":null,"lijnNummer":500,"lijnNummerPubliek":"0 ","lijnRichting":null,"lijnType":"tram","lijnTypeLink":"TRAMLIJN","omschrijving":null,"omschrijvingHighlighted":null,"richtingCode":0,"richtingCodeAndereRichting":0,"ritNummer":185500,"ritOrder":1613,"vertrekCalendar":1434350280000,"vertrekTijd":"8:38"},{"bestemming":"KNOKKE STATION","entiteitNummer":5,"gemeentes":null,"haltes":null,"id":0,"kleurAchterGrond":"#BB0022","kleurAchterGrondRand":"#BB0022","kleurVoorGrond":"#FFFFFF","kleurVoorGrondRand":"#000000","lijnGeldigVan":null,"lijnNummer":500,"lijnNummerPubliek":"0 ","lijnRichting":null,"lijnType":"tram","lijnTypeLink":"TRAMLIJN","omschrijving":null,"omschrijvingHighlighted":null,"richtingCode":0,"richtingCodeAndereRichting":0,"ritNummer":225500,"ritOrder":1605,"vertrekCalendar":1434351300000,"vertrekTijd":"8:55"},{"bestemming":"KNOKKE STATION","entiteitNummer":5,"gemeentes":null,"haltes":null,"id":0,"kleurAchterGrond":"#BB0022","kleurAchterGrondRand":"#BB0022","kleurVoorGrond":"#FFFFFF","kleurVoorGrondRand":"#000000","lijnGeldigVan":null,"lijnNummer":500,"lijnNummerPubliek":"0 ","lijnRichting":null,"lijnType":"tram","lijnTypeLink":"TRAMLIJN","omschrijving":null,"omschrijvingHighlighted":null,"richtingCode":0,"richtingCodeAndereRichting":0,"ritNummer":245500,"ritOrder":1616,"vertrekCalendar":1434352080000,"vertrekTijd":"9:08"},{"bestemming":"KNOKKE STATION","entiteitNummer":5,"gemeentes":null,"haltes":null,"id":0,"kleurAchterGrond":"#BB0022","kleurAchterGrondRand":"#BB0022","kleurVoorGrond":"#FFFFFF","kleurVoorGrondRand":"#000000","lijnGeldigVan":null,"lijnNummer":500,"lijnNummerPubliek":"0 ","lijnRichting":null,"lijnType":"tram","lijnTypeLink":"TRAMLIJN","omschrijving":null,"omschrijvingHighlighted":null,"richtingCode":0,"richtingCodeAndereRichting":0,"ritNummer":265500,"ritOrder":1604,"vertrekCalendar":1434352980000,"vertrekTijd":"9:23"}],"name":null,"omschrijvingGemeente":null,"omschrijvingHighlighted":null,"omschrijvingKort":null,"omschrijvingLang":null}';
$this->http_response_header = array(
'HTTP/1.1 200 OK',
'connection: close',
'content-language: nl-BE',
'content-type: application/json',
'date: Sun, 14 Jun 2015 23:49:47 GMT',
'p3p: CP="NON CUR OTPi OUR NOR UNI"',
'cache-control: no-cache',
'x-powered-by: Servlet/3.0',
'access-control-allow-credentials: true',
'access-control-allow-headers: Content-Type',
'access-control-allow-methods: HEAD, GET, POST, OPTIONS'
);
$result = json_decode($json, true);
switch (json_last_error()) {
case JSON_ERROR_NONE:
return $result;
break;
case JSON_ERROR_SYNTAX:
throw new Exception('Result was not valid JSON: ' . json_last_error_msg());
break;
default:
throw new Exception('Result could not be parsed as JSON: ' . json_last_error_msg());
break;
}
return null;
}
public function getHttpResponseHeader () {
return $this->http_response_header;
}
}
?>