-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtester.php
59 lines (49 loc) · 2 KB
/
tester.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
<?php if(!isset($_POST['path'])): ?>
<html>
<body>
<form action="tester.php" method="POST">
<label for="host">Host:</label><br/><input type="text" id="host" name="host" size="97"/>
<br/>
<label for="path">Path:</label><br/><input type="text" id="path" name="path" size="97"/>
<br/><br/>
<label for="post">Post/Put Content</label><br/><textarea id="post" name="post" cols="70" rows="20"></textarea>
<br/><br/>
<label for="dopost">Do post request?</label><input type="checkbox" name="dopost" checked="true" value="1" />
<br/>
<label for="doput">Do put request?</label><input type="checkbox" name="doput" value="1" />
<br/><br/>
<input type="submit" value="DO IT" />
</body>
</html>
<?php else:
$server = $_POST['host'];
$encoded = '';
$ch = curl_init($server.$_POST['path']);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLINFO_HEADER_OUT, 1);
//TODO - reset this to something more reasonable
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 1);
//TODO - add a headers option to allow for setting custom headers
$encoded = $_POST['post'];
if(isset($_POST['dopost']) && $_POST['dopost']){
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $encoded);
} else if(isset($_POST['doput']) && $_POST['doput']){
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_POSTFIELDS, $encoded);
}
$result = curl_exec($ch);
$sentHeaders = curl_getinfo($ch, CURLINFO_HEADER_OUT);
$sentContent = curl_getinfo($ch);
$content_type = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
curl_close($ch);
list($header, $body) = explode("\r\n\r\n", $result, 2);
$obj = new stdClass ;
$obj->request_text = $sentHeaders;
$obj->request = $sentContent;
$obj->response_headers = $header;
$obj->response_content = (stristr($content_type, 'json') ||stristr($content_type, 'javascript')) ? json_decode($body) : $body;
echo json_encode($obj);
endif;
?>