Skip to content

Instantly share code, notes, and snippets.

@maul-esel
Last active December 14, 2015 17:39
Show Gist options
  • Save maul-esel/5124002 to your computer and use it in GitHub Desktop.
Save maul-esel/5124002 to your computer and use it in GitHub Desktop.
A PHP page for testing web APIs, particularly for libba.net.
<?php
if (!empty($_POST)) {
$conn = curl_init();
switch ($_POST['method']) {
case 'GET': $method = CURLOPT_HTTPGET;
break;
case 'POST': $method = CURLOPT_POST;
break;
case 'PUT': $method = CURLOPT_PUT;
break;
}
# GET parameters
if ($_POST['GET']) {
$get = json_decode($_POST['GET']);
if (!strstr($_POST['url'], '?')) {
$_POST['url'] .= '?';
} else {
$_POST['url'] .= '&';
}
foreach ($get AS $k => $v) {
$_POST['url'] .= $k . '=' . $v . '&';
}
$_POST['url'] = trim($_POST['url'], '?&');
}
if ($_POST['POST'])
{
curl_setopt($conn, CURLOPT_POSTFIELDS, json_decode($_POST['POST'])); # data to upload (@ for files)
}
if ($_POST['headers']) {
curl_setopt($conn, CURLOPT_HTTPHEADER, json_decode($_POST['headers'])); # custom headers
}
curl_setopt($conn, CURLOPT_URL, $_POST['server'] . $_POST['url']); # URL
curl_setopt($conn, $method, true); # POST/GET/PUT to the URL
curl_setopt($conn, CURLOPT_RETURNTRANSFER, true); # return data, do not directly print it
curl_setopt($conn, CURLOPT_HEADER, true);
curl_setopt($conn, CURLINFO_HEADER_OUT, true);
if ($_POST['user'] && $_POST['password'])
{
curl_setopt($conn, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); # use HTTP BASIC Authentication
curl_setopt($conn, CURLOPT_USERPWD, $_POST['user'] . ':' . $_POST['password']); # set auth data
}
if ($_POST['body']) {
curl_setopt($conn, CURLOPT_POSTFIELDS, $_POST['body']);
} else if ($_POST['body-file']) {
if (!file_exists($_POST['body-file'])) {
die('Given file path "' . $_POST['body-file'] . '" does not exist!');
}
curl_setopt($conn, CURLOPT_INFILE, fopen($_POST['body-file'], 'r'));
curl_setopt($conn, CURLOPT_INFILESIZE, filesize($_POST['body-file']));
}
$response = curl_exec($conn);
list($header, $body) = explode("\r\n\r\n", $response, 2);
$code = curl_getinfo($conn, CURLINFO_HTTP_CODE);
$url = curl_getinfo($conn, CURLINFO_EFFECTIVE_URL);
$sent_headers = curl_getinfo($conn, CURLINFO_HEADER_OUT);
curl_close($conn);
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>Testing web APIs</title>
<style type='text/css'>
<!--
form {
width: 50%;
position: absolute;
left:25%;
top: 5%;
border: solid darkred 2px;
padding: 20px;
}
label, input, select, textarea, div.one-line {
display: block;
float:left;
}
label {
width: 25%;
clear: both;
}
input, select, textarea, div.one-line {
width: 74%;
}
div.one-line input, div.one-line select, div.one-line textarea {
width: 32%;
}
input[type='submit'], input[type='reset'] {
clear: left;
width: 25%;
float: right;
}
div.one-line {
float: right;
}
hr {
color: darkred;
clear: both;
margin-top: 100px;
margin-bottom: 10px;
border-style: solid;
background-color: darkred;
border-width: 1px;
}
textarea {
resize: vertical;
}
-->
</style>
<script type='text/javascript' src='//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js'></script>
<script type='text/javascript'>
<!--
var template_param;
var template_header;
function onInit() {
template_param = $('.parameter').first().clone();
template_header = $('.header').first().clone();
$('form').submit(onSubmit);
}
function onAddParameter(event) {
var clone = template_param.clone();
$('.parameter').last().after('<label>Parameter:</label>', clone);
}
function onAddHeader(event) {
var clone = template_header.clone();
$('.header').last().after('<label>Header:</label>', clone);
}
function onSubmit(event) {
var form_data = $(this).serializeArray();
var data = {};
var post = {};
var get = {};
var headers = [];
var current_param = {};
var current_header = {};
$.each(form_data, function(i, item) {
if (item['name'] == 'param-name' || item['name'] == 'param-value' || item['name'] == 'param-method') {
current_param[item['name']] = item['value'];
}
else if (item['name'] == 'header-name' || item['name'] == 'header-value') {
current_header[item['name']] = item['value'];
}
else {
data[item['name']] = item['value'];
}
if (item['name'] == 'param-method') {
if (current_param['param-method'] == 'GET' && current_param['param-name']) {
get[current_param['param-name']] = current_param['param-value'];
} else if (current_param['param-method'] == 'POST' && current_param['param-name']) {
post[current_param['param-name']] = current_param['param-value'];
}
current = {};
}
else if (item['name'] == 'header-value' && current_header['header-name']) {
headers.push(current_header['header-name'] + ': ' + current_header['header-value']);
}
});
data['GET'] = JSON.stringify(get);
data['POST'] = JSON.stringify(post);
data['headers'] = JSON.stringify(headers);
post_to_url('', data, 'POST');
event.preventDefault();
}
function post_to_url(path, params, method) {
method = method || "post"; // Set method to post by default, if not specified.
// The rest of this code assumes you are not using a library.
// It can be made less wordy if you use one.
var form = document.createElement("form");
form.setAttribute("method", method);
form.setAttribute("action", path);
for(var key in params) {
if(params.hasOwnProperty(key)) {
var hiddenField = document.createElement("input");
hiddenField.setAttribute("type", "hidden");
hiddenField.setAttribute("name", key);
hiddenField.setAttribute("value", params[key]);
form.appendChild(hiddenField);
}
}
document.body.appendChild(form);
form.submit();
}
-->
</script>
</head>
<body onload='onInit()'>
<form action='' method='POST' name='data-form'>
<?php if (!empty($_POST)) {
echo '<fieldset><legend>Output:</legend>',
'<label>URL:</label><input type="text" value="' . $url . '"/>',
'<label>Sent Headers:</label><textarea>' . $sent_headers . '</textarea>',
'<label>Status Code:</label><input type="text" value="' . $code . '"/>',
'<label>Headers:</label><textarea>' . $header . '</textarea>',
'<label>Body:</label><textarea>' . $body . '</textarea>',
'</fieldset><hr/>';
} ?>
<label>Server:</label>
<input name='server' type='url' value='http://experimental.api.libba/'/>
<label>URL:</label>
<input name='url' type='text'/>
<label>Method:</label>
<select name='method'>
<option>GET</option>
<option>POST</option>
<option>PUT</option>
<option>DELETE</option>
</select>
<hr/>
<label>Header:</label>
<div class='one-line header'>
<input name='header-name' type='text' placeholder='name'/>
<input name='header-value' type='text' placeholder='value'/>
</div>
<div class='one-line'>
<input type='button' value='Add header' onclick='onAddHeader(event)'/>
</div>
<hr/>
<label>User:</label>
<input name='user' type='text'/>
<label>Password:</label>
<input name='password' type='password'/>
<hr>
<label>Parameter:</label>
<div class='one-line parameter'>
<input name='param-name' type='text' placeholder='name'/>
<input name='param-value' type='text' placeholder='value'/>
<select name='param-method'>
<option>GET</option>
<option>POST</option>
</select>
</div>
<div class='one-line'>
<input type='button' value='Add parameter' onclick='onAddParameter(event)'/>
</div>
<hr/>
<label>Body:</label>
<textarea name='body'></textarea>
<label>Body (file) :</label>
<input name='body-file' type='text'/>
<hr/>
<input type='submit'/>
<input type='reset'/>
</form>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment