ns.class.php

Gesponsorde koppelingen

PHP script bestanden

  1. ns.class.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
<?php
/*
 * Author Gerhard Lubbers
 * 21-3-2012
 * Version 1.0
 */

/*
 * NS class
 *
 * Allows the user to easily retrieve information from http://www.ns.nl.
 * Requirements: NS API username and password, get them here: https://www.ns.nl/ews-aanvraagformulier/
 */

class NS{
    
    /*
    * cUrl Class
    * @var cUrl handler
    */

    private $curl;
    
    /*
    * ns.nl API request URL's
    */
        
    // De v2 versie van get stations bevat extra info

    const getStations         = 'http://webservices.ns.nl/ns-api-stations-v2';
    //const getStations         = 'http://webservices.ns.nl/ns-api-stations';
    
    const getDepartures     = 'http://webservices.ns.nl/ns-api-avt';
    const getPrices         = 'http://webservices.ns.nl/ns-api-prijzen-v2';
    const getAdvise         = 'http://webservices.ns.nl/ns-api-treinplanner';
    const getDisturbances     = 'http://webservices.ns.nl/ns-api-storingen';
    
    /*
    * @param required class $curl -> set class curl
    */

    public function __construct( $curl ){
        
        $this->curl = $curl;
    }
    
    
    
    /*
    * getStations
    * Retrieve all stations
    * @return xml element with all stations
    */

    public function getStations(){
        
        return new SimpleXMLElement( $this->curl->get( self::getStations ) );
    }

    
    /*
    * getDepartures
    * Retrieve departures for specified station
    * @param required string $station -> station short/middle/full name, or synonymous
    * @return xml element with departures from specified station
    */

    public function getDepartures( $station ){
        
        // cURL parameters
        $parameters = array(
            'station' => $station
        );
        
        // make request and return xml
        return new SimpleXMLElement( $this->curl->get( self::getDepartures, $parameters ) );
    }

    
    /*
    * getPrices
    * Retrieve prices for specified track
    * @param required string $from -> station short/middle/full name, or synonymous
    * @param required string $to -> station short/middle/full name, or synonymous
    * @param string $via -> station short/middle/full name, or synonymous
    * @param date $dateTime -> date in ddMMyyyy
    * @return xml element with prices for track $from to $to via $via on $dateTime
    */
    
    public function getPrices ($from, $to, $via = '', $dateTime = ''){
        
        // cURL parameters
        $parameters = array(
            'from'    => $from,
            'to'     => $to
        );
        
        // optional parameters
        if($via){         $parameters['via'] = $via; }
        if($dateTime){     $parameters['dateTime'] = $dateTime; }
        
        // make request and return xml
        return new SimpleXMLElement( $this->curl->get( self::getPrices, $parameters ) );
    }

    
    /*
    * getAdvise
    * Retrieve travel advise for specified track
    * @param required string $fromStation -> station short/middle/full name, or synonymous
    * @param required string $toStation -> station short/middle/full name, or synonymous
    * @param string $viaStation -> station short/middle/full name, or synonymous
    * @param int $previousAdvices -> advises in the past, not guaranteed, max 5
    * @param int $nextAdvices -> advises in the future, not guaranteed, max 5
    * @param date $dateTime -> date in ddMMyyyy
    * @param boo $departure -> if $depature is true, dateTime is departure time, else dateTime is arrival time
    * @param boo $hslAllowed -> may travel advise contain high-speed trains, default true
    * @param boo $yearCard -> if user has yearcard, some advises will be more expensive but shorter, default false
    * @return xml element with advises for track $from to $to via $via on $dateTime
    */
    
    public function getAdvise ($fromStation, $toStation, $viaStation = '', $previousAdvices = '', $nextAdvices = '', $dateTime = '', $departure = '', $hslAllowed = '', $yearCard = ''){
        
        // cURL parameters
        $parameters = array(
            'fromStation'     => $fromStation,
            'toStation'     => $toStation
        );
        
        // optional parameters
        if($viaStation){         $parameters['viaStation'] =         $viaStation; }
        if($previousAdvices){    $parameters['previousAdvices'] =     $previousAdvices; }
        if($nextAdvices){         $parameters['nextAdvices'] =         $nextAdvices; }
        if($dateTime){             $parameters['dateTime'] =             $dateTime; }
        if($departure){         $parameters['departure'] =             $departure; }
        if($hslAllowed){        $parameters['hslAllowed'] =         $hslAllowed; }
        if($yearCard){             $parameters['yearCard'] =             $yearCard; }
        
        // make request and return xml
        return new SimpleXMLElement( $this->curl->get( self::getAdvise, $parameters ) );
    }

    
    /*
    * getDisturbances
    * Retrieve Disturbances
    * @param string $station -> station short/middle/full name, or synonymous
    * @param boo $actual -> $actual is true will return actual disturbances, plannend and unplanned
    * @param boo $unplanned -> $unplanned is true will return planned disturbances
    * @return xml element with disturbances for specified station
    */
    
    public function getDisturbances($station = '', $actual = '', $unplanned = ''){
        
        // optional parameters
        $parameters = array();
        if($station){     $parameters['station'] =     $station; }
        if($actual){     $parameters['actual'] =     $actual; }
        if($unplanned){ $parameters['unplanned'] =     $unplanned; }
        
        // make request and return xml
        return new SimpleXMLElement( $this->curl->get( self::getDisturbances, $parameters ) );
    }
}


/*
* user class
*
* NS API username and password controller
* @param required string $username, NS API username
* @param required string $password, NS API password
*/

class user{
    
    /*
    * @var $username
    * NS API username
    */

    private $username;
        
    /*
    * @var $password
    * NS API password
    */
    
    private $password;
    
    /*
    * set $username and $passowrd
    */

    public function __construct($username, $password){
        
        $this->username = $username;
        $this->password = $password;
    }

    
    /*
    * getUsername
    * @return NS API username
    */
    
    public function getUsername(){
    
        return $this->username;
    }
    
    
    /*
    * getPassword
    * @return NS API password
    */
    
    public function getPassword(){
    
        return $this->password;
    }
}


/*
* cURL class
*
* Sends cURL request to the NS API and returns cURL response
* @param required $user, user class
*/

class curl{
    
    /*
    * @var $user
    * User class
    */

    private $user;
    
    /*
    * set user
    */

    public function __construct( $user ){
    
        $this->user = $user;
    }

    
    /*
    * get
    * Sends get request
    * @param required string $url -> request url
    * @param array $parameters -> paramaters for the request
    */

    public function get( $url, $parameters = array() ){
        
        // if there are parameters, grab them
        if(!empty($parameters)){
            $url .= '?' . http_build_query($parameters);
        }

        
        // set up cURL requets
        $ch = curl_init( $url );
        curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
        // grab username and password
        curl_setopt($ch, CURLOPT_USERPWD,  $this->user->getUsername().":". $this->user->getPassword());
        curl_setopt($ch, CURLOPT_TIMEOUT, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        // send the requets
        $xml = curl_exec($ch);
        // close cURL
        curl_close($ch);
        // return the cURL response
        return $xml;
    }
}

?>

 
 

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.