Skip to content

Instantly share code, notes, and snippets.

@tleilax
Forked from luniki/halt.php
Created July 19, 2013 11:34
Show Gist options
  • Save tleilax/6038516 to your computer and use it in GitHub Desktop.
Save tleilax/6038516 to your computer and use it in GitHub Desktop.
<?
function words($string)
{
return preg_split('/ /', $string, -1, PREG_SPLIT_NO_EMPTY);
}
function halt($status = 200, $headers = array(), $body = '')
{
$args = func_get_args();
$result = array();
$constraints = array(
'status' => 'is_int',
'headers' => 'is_array',
'body' => 'is_string',
);
foreach ($constraints as $state => $constraint) {
if ($constraint(current($args))) {
$result[$state] = array_shift($args);
}
}
echo json_encode(func_get_args()) . "\n";
echo json_encode($result) . "\n\n";
}
header('Content-Type: text/plain');
halt();
// => []
halt(201);
// => {"status":201}
halt("Done");
// => {"body":"Done"}
halt(100, "Continue");
// => {"status":100,"body":"Continue"}
halt(301, array('Location'=>'y'));
// => {"status":301,"headers":{"Location":"y"}}
halt(302, "Found");
// => {"status":302,"body":"Found"}
halt(303, array('Location'=>'y'), 'See other');
// => {"status":303,"headers":{"Location":"y"},"body":"See other"}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment