-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproxy.php
executable file
·53 lines (43 loc) · 1.66 KB
/
proxy.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
<?php
ini_set('display_errors', 'On');
error_reporting(E_ALL);
$url = file_get_contents('php://input');
echo json_encode(getHttpStatus($url));
function getHttpStatus($url){
$status = doRequest($url);
// if we got an redirect, follow until there is no redirect any more
if($status['status'] == 302 OR $status['status'] == 301){
$status_new = $status;
while($status_new['status']==302 OR $status_new['status']==301){
$status_new = doRequest($status_new['target']);
$httpstatuscode = $status_new['status'];
if($httpstatuscode==302 OR $httpstatuscode==301){
$status['target'].=" <span class='inline-status code_{$httpstatuscode}'>{$httpstatuscode}</span> →<br />".$status_new['target'];
} else {
$status['target'].=" <span class='inline-status final-status code_{$httpstatuscode}'>{$httpstatuscode}</span>";
}
}
}
return $status;
}
function doRequest($url){
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, true); // we want headers
curl_setopt($ch, CURLOPT_NOBODY, true); // we don't need body
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_TIMEOUT,10);
$headers = curl_exec($ch);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
// Check if there's a Location: header (redirect)
if (preg_match('/^Location: (.+)$/im', $headers, $matches)){
$target = trim($matches[1]);
} else {
$target ="";
}
$values['url'] = $url;
$values['status'] = $httpcode;
$values['target'] = $target;
return $values;
}
?>