-
Notifications
You must be signed in to change notification settings - Fork 2
/
fetch.php
45 lines (39 loc) · 1.17 KB
/
fetch.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
<?php
function buildContext() {
$opts = array(
'http'=>array(
'method'=>"GET",
'header'=>"User-Agent: " . $_SERVER['HTTP_USER_AGENT'] . "\r\n" .
"Accept: */*\r\n" .
"Accept-Language: fr,fr-FR;q=0.8,en-US;q=0.5,en;q=0.3\r\n" .
"DNT: 1\r\n" .
"Referer: " . $_SERVER['HTTP_REFERER'] ."\r\n"
)
);
$context = stream_context_create($opts);
return $context;
}
function error_400($msg) { // Quick & dirty
header("HTTP/1.0 400 Bad Request");
http_response_code(400);
echo json_encode(array("error" => $msg));
exit();
}
function error_500($msg) { // Quick & dirty
header("HTTP/1.0 500 Internal Server Error");
http_response_code(500);
echo json_encode(array("error" => $msg));
exit();
}
$url = filter_input(INPUT_GET, 'url', FILTER_VALIDATE_URL);
if ($url === FALSE)
error_400("Parameter url invalid");
try {
error_reporting(0);
$file = file_get_contents($url, false, buildContext());
if ($file === FALSE)
error_500("Could not fetch data");
echo $file;
} catch (Exception $e) {
error_500("Unknown error");
}