ns.class.php

Gesponsorde koppelingen

PHP script bestanden

  1. ns.class.php
  2. example.php

« Lees de omschrijving en reacties

Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
<?php
/**
 * Author gerhard.dev
 * 13-02-2020
 * Version 1.0
 */

class NSAPI
{
    private $apiKey;
    private $baseUrl = 'https://gateway.apiportal.ns.nl/';
    private $stations = [];

    /**
     * constructor
     * Set NS API key, get your API key here: https://apiportal.ns.nl
     *
     * @param required string $strApiKey
     */

    public function __construct($strApiKey)
    {

        $this->apiKey = $strApiKey;
    }


    /**
     * loadStations
     * Retrieve and store all stations, so we only have to query them once
     */

    private function loadStations()
    {

        if(empty($this->stations))
        {

            $aStations = $this->request('public-reisinformatie/api/v2/stations')->payload;
            if($aStations && !empty($aStations))
            {

                foreach($aStations as $oStation)
                {

                    $this->stations[strtolower($oStation->code)] = $oStation;
                }
            }
        }


        if(empty($this->stations))
            throw new Exception('Failed to load stations');
    }


    /**
     * getStation
     * Retrieve station info
     *
     * @param required string $strSearch -> search by station name, (UIC, EVA, Station) code
     *
     * @return object station info
     */

    public function getStation($strSearch)
    {

        $strSearch = strtolower($strSearch);
        $this->loadStations();

        if(isset($this->stations[$strSearch]))
            return $this->stations[$strSearch];
        else
        {
            foreach($this->stations as $oStation)
            {

                if(
                    $oStation->UICCode == $strSearch ||
                    $oStation->EVACode == $strSearch ||
                    strtolower($oStation->code) == $strSearch ||
                    strtolower($oStation->namen->lang) == $strSearch ||
                    strtolower($oStation->namen->kort) == $strSearch ||
                    strtolower($oStation->namen->middel) == $strSearch
                )
                return $oStation;
            }
        }

        throw new Exception('Invalid station: '. $strStation);
    }


    /**
     * getStations
     * Retrieve list of stations
     *
     * @return array list of stations
     */

    public function getStations()
    {

        $this->loadStations();

        return $this->stations;
    }


    /**
     * getArrivals
     * Retrieve list of arrivals for specific station
     *
     * @param string $strStation -> station name or code
     * @param (optional) string $strDateTime -> return arrivals starting from this given dateTime
     * @param (optional) int $iMaxJourneys -> Max number of arrivals to return
     * @param (optional) string $strLang -> Language to use for translatable messages
     *
     * @return array list of arrivals
     */

    public function getArrivals($strStation, $strDateTime = '', $iMaxJourneys = 0, $strLang = '')
    {

        $aParameters = [];
        if(!empty($strStation))
            $aParameters['station'] = $this->getStation($strStation)->code;

        if(!empty($strDateTime))
            $aParameters['dateTime'] = date('c', strtotime($strDateTime));

        if(!empty($strLang))
            $aParameters['lang'] = $strLang;

        if(!empty($iMaxJourneys) && (int) $iMaxJourneys > 0)
            $aParameters['maxJourneys'] = (int) $iMaxJourneys;

        return $this->request('public-reisinformatie/api/v2/arrivals', $aParameters)->payload->arrivals;
    }


    /**
     * getDepartures
     * Retrieve list of departures for specific station
     *
     * @param string $strStation -> station name or code
     * @param (optional) string $strDateTime -> return departures starting from this given dateTime
     * @param (optional) int $iMaxJourneys -> Max number of departures to return
     * @param (optional) string $strLang -> Language to use for translatable messages
     *
     * @return array list of departures
     */

    public function getDepartures($strStation, $strDateTime = '', $iMaxJourneys = 0, $strLang = '')
    {

        $aParameters = [];
        if(!empty($strStation))
            $aParameters['station'] = $this->getStation($strStation)->code;

        if(!empty($strDateTime))
            $aParameters['dateTime'] = date('c', strtotime($strDateTime));

        if(!empty($strLang))
            $aParameters['lang'] = $strLang;

        if(!empty($iMaxJourneys) && (int) $iMaxJourneys > 0)
            $aParameters['maxJourneys'] = (int) $iMaxJourneys;

        return $this->request('public-reisinformatie/api/v2/departures', $aParameters)->payload->departures;
    }


    /**
     * getDisruption
     * Retrieve specific disruption info
     *
     * @param string $strDisruption -> Dispruption ID
     *
     * @return object disruption info
     */

    public function getDisruption($strDisruptionId)
    {

        return $this->request('public-reisinformatie/api/v2/disruptions/'. $strDisruptionId)->payload;
    }


    /**
     * getDisruptions
     * Retrieve list of disruptions
     *
     * @param (optional) boolean $bActual -> If true, only the disruptions from the last 2 hours will be returned
     * @param (optional) string $strType -> WERKZAAMHEID or STORING, both will be returned by default
     * @param (optional) string $strLang -> Language to use for translatable messages
     *
     * @return array list of disruptions
     */

    public function getDisruptions($bActual = true, $strType = '', $strLang = '')
    {

        if(!empty($strType))
            $aParameters['type'] = strtolower($strType);

        $aParameters['actual'] = !!$bActual;

        if(!empty($strLang))
            $aParameters['lang'] = $strLang;

        return $this->request('public-reisinformatie/api/v2/disruptions', $aParameters)->payload;
    }


    /**
     * getStationDisruptions
     * Retrieve list of disruptions for specific station
     *
     * @param string $strStation -> station name or code
     *
     * @return array list of disruptions
     */

    public function getStationDisruptions($strStation)
    {

        return $this->request('public-reisinformatie/api/v2/disruptions/station/'. $this->getStation($strStation)->code)->payload;
    }


    /**
     * getTrips
     * Retrieve list of available trips based on the travel data provided
     *
     * @param string $strFrom -> station name or code to travel from
     * @param string $strTo -> station name or code to travel to
     * @param (optional) string $strVia -> station name or code to travel through
     * @param (optional) string $strDateTime -> return trips starting from this given dateTime
     * @param (optional) boolean $bDepature -> If false $strDateTime is the time of arrival
     * @param (optional) string $strClass -> Class of travel, first or second
     * @param (optional) string $strLang -> Language to use for translatable messages
     *
     * @return array list of trips
     */

    public function getTrips($strFrom, $strTo, $strVia = '', $strDateTime = '', $bDepature = true, $strClass = '', $strLang = '')
    {

        $aParameters = [];
        if(!empty($strFrom))
            $aParameters['fromStation'] = $this->getStation($strFrom)->code;

        if(!empty($strTo))
            $aParameters['toStation'] = $this->getStation($strTo)->code;

        if(!empty($strVia))
            $aParameters['viaStation'] = $this->getStation($strVia)->code;

        if(!empty($strDateTime))
        {

            $aParameters['dateTime'] = date('c', strtotime($strDateTime));
            $aParameters['departure'] = !!$bDepature;
        }


        if(!empty($strClass))
        {

            $strClass = strtoupper($strClass);
            if($strClass == 'FIRST' || $strClass == 'SECOND')
                $strClass .= '_CLASS';

            $aParameters['travelClass'] = $strClass;
        }


        if(!empty($strLang))
            $aParameters['lang'] = $strLang;

        return $this->request('public-reisinformatie/api/v3/trips', $aParameters)->trips;
    }


    /**
     * getRequestInfo
     * Retrieve parameters from a request
     *
     * @param string $strCtx -> ctxRecon
     *
     * @return object request info
     */

    public function getRequestInfo($strCtx)
    {

        $aRequest = [];
        $aCtx = explode('|', $strCtx);
        if(!empty($aCtx))
        {

            foreach($aCtx as $strCtxPart)
            {

                $aCtxPart = explode('=', $strCtxPart);
                if(count($aCtxPart) == 2)
                    $aRequest[$aCtxPart[0]] = strpos($aCtxPart[0], 'Station') !== false ? $this->getStation($aCtxPart[1]) : $aCtxPart[1];
            }
        }

        return (object) $aRequest;
    }


    /**
     * request
     * Fires request and handles the response if they contain errors
     *
     * @param string $strEndpoint -> endpoint to request
     * @param (optional) array $aParameters -> array of url parameters to send with the request
     */

    public function request($strEndpoint, $aParameters = [])
    {

        $strUrl = $this->baseUrl . $strEndpoint;

        if(!empty($aParameters))
            $strUrl .= '?' . http_build_query($aParameters);

        $ch = curl_init($strUrl);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, ['Ocp-Apim-Subscription-Key:'. $this->apiKey]);
        $response = curl_exec($ch);
        curl_close($ch);

        $response = json_decode($response);
        $strDefaultError = 'Something went wrong..';

        if(isset($response->statusCode) && $response->statusCode != 200)
        {

            throw new Exception(isset($response->message) ? $response->message : $strDefaultError);
        }

        elseif(isset($response->code) && $response->code != 200)
        {

            $aErrors = [];
            if(isset($response->errors) && !empty($response->errors))
            {

               foreach($response->errors as $oError)
               {

                   $aErrors[] = $oError->message;
               }              
            }

            throw new Exception(empty($aErrors) ? $strDefaultError : implode(', ', $aErrors));
        }

        return $response;
    }
}

?>

 
 

Om de gebruiksvriendelijkheid van onze website en diensten te optimaliseren maken wij gebruik van cookies. Deze cookies gebruiken wij voor functionaliteiten, analytische gegevens en marketing doeleinden. U vindt meer informatie in onze privacy statement.