Created
October 25, 2013 15:46
-
-
Save DaveRandom/7156802 to your computer and use it in GitHub Desktop.
Fetch a remote URL and show some stuff. Just your basic waste on an hour.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// hack for viper7 | |
$_SERVER['REQUEST_URI'] = '/' . basename($_SERVER['PHP_SELF']) . '/55dev'; | |
session_start(); | |
$methods = [ | |
'GET', | |
'HEAD', | |
'POST', | |
'PUT', | |
]; | |
if (!empty($_POST['uri'])) { | |
do { | |
$error = 'No error'; | |
$head = $body = $responseCode = $responseText = $headOut = ''; | |
$headers = []; | |
$uri = $_POST['uri']; | |
$method = empty($_POST['custmethod']) ? $methods[$_POST['method']] : $_POST['custmethod']; | |
$redirects = !empty($_POST['redirects']); | |
$req_head = $_POST['headers']; | |
$req_body = $_POST['body']; | |
$opts = [ | |
CURLOPT_SSL_VERIFYPEER => false, | |
CURLOPT_FAILONERROR => false, | |
CURLOPT_RETURNTRANSFER => true, | |
CURLOPT_HEADER => true, | |
CURLOPT_HEADER_OUT => true, | |
CURLOPT_FOLLOWLOCATION => $redirects, | |
CURLOPT_CUSTOMREQUEST => $method, | |
]; | |
if (!empty($req_head)) { | |
foreach (preg_split('/\r?\n(?![ \t])/', $req_head, -1, PREG_SPLIT_NO_EMPTY) as $header) { | |
list($key, $val) = preg_split('/\s*:\s*/', $header, 2); | |
$headers[strtolower($key)][] = $val; | |
} | |
$headers['connection'] = ['close']; | |
} | |
if (!empty($req_body)) { | |
$headers['content-length'] = [strlen($req_body)]; | |
$opts[CURLOPT_POSTFIELDS] = $req_body; | |
} | |
if ($headers) { | |
$opts[CURLOPT_HTTPHEADER] = []; | |
foreach ($headers as $name => $vals) { | |
$name = preg_replace_callback('/(?:^|-)[a-z]/', function($match) { return $match[0] & "\xDF"; }, $name); | |
foreach ($vals as $val) { | |
$opts[CURLOPT_HTTPHEADER][] = $name . ':' . trim($val); | |
} | |
} | |
} | |
if (!$ch = curl_init($uri)) { | |
$error = 'curl_init() failed'; | |
break; | |
} | |
foreach ($opts as $opt => $val) { | |
if (!curl_setopt($ch, $opt, $val)) { | |
$error = 'curl_setopt() failed: ' . curl_error($ch); | |
break; | |
} | |
} | |
if (!$result = curl_exec($ch)) { | |
$error = 'curl_exec() failed: ' . curl_error($ch); | |
break; | |
} | |
$headOut = trim(curl_getinfo($ch, CURLINFO_HEADER_OUT)); | |
list($head, $body) = preg_split('/\r?\n\r?\n/', curl_exec($ch), 2); | |
if (preg_match('#^HTTP/1\.[01] (\d{3}) ([^\r\n]+)#', $head, $matches)) { | |
list(, $responseCode, $responseText) = $matches; | |
} else { | |
$error = 'HTTP response invalid'; | |
break; | |
} | |
} while (false); | |
$_SESSION = [ | |
'uri' => $uri, | |
'method' => $method, | |
'redirects' => $redirects, | |
'error' => $error, | |
'req_headout' => $headOut, | |
'req_head' => $req_head, | |
'req_body' => $req_body, | |
'res_head' => $head, | |
'res_body' => $body, | |
'response_code' => $responseCode, | |
'response_text' => $responseText, | |
]; | |
header('HTTP/1.1 303 See Other'); | |
header('Location: http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']); | |
exit; | |
} | |
if (empty($_SESSION)) { | |
$_SESSION = [ | |
'uri' => '', | |
'method' => '', | |
'redirects' => '', | |
'error' => '', | |
'req_headout' => '', | |
'req_head' => '', | |
'req_body' => '', | |
'res_head' => '', | |
'res_body' => '', | |
'response_code' => '', | |
'response_text' => '', | |
]; | |
} | |
$cust = true; | |
?> | |
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>HTTP tester</title> | |
</head> | |
<body> | |
<?php if (!empty($_SESSION['uri'])) { ?> | |
<h1>Results: <?=htmlspecialchars($_SESSION['uri'])?></h1> | |
<table> | |
<tr> | |
<td>Error status</td> | |
<td><?=htmlspecialchars($_SESSION['error'])?></td> | |
</tr> | |
<tr> | |
<td>Response code</td> | |
<td><?=$_SESSION['response_code']?></td> | |
</tr> | |
<tr> | |
<td>Status text</td> | |
<td><?=htmlspecialchars($_SESSION['response_text'])?></td> | |
</tr> | |
</table> | |
<h3>Request Headers</h3> | |
<pre><?=htmlspecialchars($_SESSION['req_headout'])?></pre> | |
<h3>Response Headers</h3> | |
<pre><?=htmlspecialchars($_SESSION['res_head'])?></pre> | |
<h3>Response Body</h3> | |
<pre><?=htmlspecialchars($_SESSION['res_body'])?></pre> | |
<hr> | |
<?php } ?> | |
<h3>Request details</h3> | |
<form action="<?=$_SERVER['REQUEST_URI']?>" method="post"> | |
<table> | |
<tr> | |
<td>URI</td> | |
<td><input type="text" size="30" name="uri" value="<?=htmlspecialchars($_SESSION['uri'])?>"></td> | |
</tr> | |
<tr> | |
<td>Method</td> | |
<td> | |
<select name="method"> | |
<?php foreach ($methods as $key => $name) { ?> | |
<option value="<?=$key?>"<?php if ($name == $_SESSION['method']) { echo ' selected'; $cust = false; } ?>><?=$name?></option> | |
<?php } ?> | |
</select> | |
<span>or custom method: <input type="text" size="7" name="custmethod" value="<?=$cust ? htmlspecialchars($_SESSION['method']) : ''?>"></span> | |
</td> | |
</tr> | |
<tr> | |
<td>Follow redirects</td> | |
<td><input type="checkbox" name="redirects" value="1"<?php if ($_SESSION['redirects']) echo ' checked';?>></td> | |
</tr> | |
</table> | |
<div>Request headers (1 per line)</div> | |
<div><textarea name="headers" rows="8" cols="80"><?=htmlspecialchars($_SESSION['req_head'])?></textarea></div> | |
<div>Request body</div> | |
<div><textarea name="body" rows="8" cols="80"><?=htmlspecialchars($_SESSION['req_body'])?></textarea></div> | |
<div><input type="submit" value="Go"></div> | |
</form> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment