-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathWSIVMissionsNextRequest.php
130 lines (115 loc) · 4.39 KB
/
WSIVMissionsNextRequest.php
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
<?php
class WSIVMissionsNextRequest {
private $request;
private $requestFilename;
private $location;
private $uri;
private $action;
private $version;
private $client;
private $station;
private $direction;
private $date;
private $line;
private $return;
private $perturbations;
private $missions;
private $expire;
public function __construct($idLine, $stationName, $directionSens, $proxyHost = null, $proxyPort = null) {
$this->expire = time() - 30;
$this->maxByDay = 1000*1000*100;
$this->location = "http://opendata-tr.ratp.fr/wsiv/services/Wsiv?wsdl=";
$this->uri = "http://opendata-tr.ratp.fr/wsiv/services";
$this->action = "urn:getMissionsNext";
$this->version = 0;
$this->proxyHost = $proxyHost;
$this->proxyPort = $proxyPort;
if ($this->proxyHost) {
$this->client = new SoapClient(null, array(
'location' => $this->location,
'uri' => "",
'proxy_host' => $this->proxyHost,
'proxy_port' => $this->proxyPort
));
} else {
$this->client = new SoapClient(null, array(
'location' => $this->location,
'uri' => ""
));
}
$this->requestFilename = 'cache/' . $idLine . '-' . $stationName . '-' . $directionSens;
$this->request = '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://wsiv.ratp.fr/xsd" xmlns:wsiv="http://wsiv.ratp.fr">
<soapenv:Header/>
<soapenv:Body>
<wsiv:getMissionsNext>
<wsiv:station>
<xsd:line>
<xsd:id>' . $idLine .'</xsd:id>
</xsd:line>
<xsd:name>' . $stationName . '</xsd:name>
</wsiv:station>
<wsiv:direction>
<xsd:sens>' . $directionSens . '</xsd:sens>
</wsiv:direction>
</wsiv:getMissionsNext>
</soapenv:Body>
</soapenv:Envelope>';
$xml = $this->getXML();
$this->setAttributesFromXML($xml);
}
public function getReturn() {
return $this->return;
}
public function getStation() {
return $this->station;
}
public function getDirection() {
return $this->direction;
}
public function getTime() {
return strtotime($this->date);
}
public function getLine() {
return $this->line;
}
public function getPerturbations() {
return $this->perturbations;
}
public function getMissions() {
return $this->missions;
}
private function getXML() {
if(file_exists($this->requestFilename) && filemtime($this->requestFilename) > $this->expire) {
$clean_xml = file_get_contents($this->requestFilename);
$xml = simplexml_load_string($clean_xml);
return $xml;
}
if (!$this->incrementAndCheckIsAllowed()) {
echo "I've had too much today, sorry."; die;
}
$xmlstring = $this->client->__doRequest($this->request, $this->location, $this->action, $this->version);
$clean_xml = str_ireplace(['SOAPENV:', 'NS1:', 'NS2:'], '', $xmlstring);
file_put_contents($this->requestFilename, $clean_xml);
$xml = simplexml_load_string($clean_xml);
return $xml;
}
private function setAttributesFromXML($xml) {
$this->return = $xml->Body->getMissionsNextResponse->return;
$this->station = $this->return->argumentStation->name;
$this->direction = $this->return->argumentDirection->name;
$this->date = $this->return->argumentDate;
$this->line = $this->return->argumentLine->reseau->name . ' ' . $this->return->argumentLine->code;
$this->perturbations = $this->return->perturbations;
$this->missions = $this->return->missions;
}
private function incrementAndCheckIsAllowed() {
$filename = "logs/" . date("Ymd");
if (!file_exists($filename)) {
file_put_contents($filename, 1);
return true;
}
$count = file_get_contents($filename);
file_put_contents($filename, 1 + (int)$count);
return ($count <= $this->maxByDay);
}
}