-
Notifications
You must be signed in to change notification settings - Fork 1
/
relay.php
67 lines (59 loc) · 1.75 KB
/
relay.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
<?php
/**
* (c) Kjeld Borch Egevang
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
ini_set('display_errors', 1);
error_reporting(E_ALL | E_STRICT);
class Relay
{
public function DoCurl($url, $data, $method)
{
$ch = curl_init();
if ($data && $method == 'GET') {
$filter = $data['filter'];
unset($data['filter']);
$query = http_build_query($data);
$query .= '&filter='.$filter;
$url .= '?'.$query;
}
if ($data && $method == 'POST') {
$json = json_encode($data, JSON_UNESCAPED_UNICODE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
}
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_FAILONERROR, true);
$data = curl_exec($ch);
if (curl_errno($ch)) {
$this->info = curl_getinfo($ch);
$error = curl_error($ch);
if ($error) {
$this->error = $error;
} else {
$this->error = "Connection failed";
}
curl_close($ch);
return false;
} else {
curl_close($ch);
return $data;
}
}
}
$url = urldecode($_SERVER['QUERY_STRING']);
$elspot = 'https://api.energidataservice.dk/dataset/Elspotprices';
if (substr($url, 0, strlen($elspot)) == $elspot) {
$relay = new Relay();
$method = 'GET';
$data = [];
$json = $relay->DoCurl($url, $data, $method, null);
print $json;
} else {
die('Not relayed');
}
?>