Skip to main content

Request & Response

JSON API Example

router(function() {
// Return JSON data
route(method(GET), url_path("/api/users"), function() {
$users = [
["id" => 1, "name" => "Alice"],
["id" => 2, "name" => "Bob"],
];
render(200, json_out($users));
});

// Accept JSON input
route(method(POST), url_path("/api/users"), function() {
$data = json_in();
// $data contains the parsed JSON body
render(201, json_out(["created" => $data]));
});

// Read request headers
route(method(GET), url_path("/api/protected"), function() {
$token = request_header("Authorization");
if (empty($token)) {
render(401, json_out(["error" => "Unauthorized"]));
return;
}
render(200, json_out(["message" => "Welcome!"]));
});
});

Content Negotiation

route(method(GET), url_path("/"), function() {
// Check Accept header and respond accordingly
if (accept(JSON_CONTENT)) {
render(200, json_out(["hello" => "world"]));
return;
}

if (accept(HTML_CONTENT)) {
render(200, html_out("<h1>Hello World</h1>"));
return;
}

if (accept(XML_CONTENT)) {
render(200, content(XML_CONTENT, "<message>Hello World</message>"));
return;
}

throw new Exception("Not Acceptable", 406);
});

Custom Headers

route(method(GET), url_path("/download"), function() {
$fileContent = file_get_contents("report.pdf");

render(200, content(PDF_CONTENT, $fileContent), [
"Content-Disposition" => "attachment; filename=\"report.pdf\"",
"Cache-Control" => "no-cache",
]);
});

route(method(GET), url_path("/api/data"), function() {
// Set individual response headers
response_header("X-Custom-Header", "custom-value");
response_header("Cache-Control", "max-age=3600");

render(200, json_out(["data" => "value"]));
});

Redirects

route(method(GET), url_path("/old-page"), function() {
redirect("/new-page");
});

route(method(POST), url_path("/form-submit"), function() {
$data = $_POST;
// Process form...
redirect("/thank-you");
});

How It Works

TeensyPHP provides simple functions for handling HTTP requests and responses:

  • Request functions read incoming data (headers, JSON body)
  • Response functions send data back with appropriate content types and status codes
  • Content negotiation lets you respond with different formats based on the client's Accept header

The render() function is the primary way to send responses, combining status code, content, and optional headers.

Function Reference

Request Functions

FunctionDescription
request_header(string $header): ?stringGet a request header value
json_in(): arrayParse JSON request body
accept(string $contentType): boolCheck if client accepts content type

Response Functions

FunctionDescription
render(int $code, string $content, array $headers = []): voidSend response with status code and content
json_out(array $data): stringEncode array as JSON string
html_out(string $content): stringWrap content with HTML content type
content(string $type, string $content): stringSet content type and return content
redirect(string $url): voidSend redirect response
response_header(string $header, string $value): voidSet a response header

Content Type Constants

ConstantContent Type
JSON_CONTENTapplication/json
HTML_CONTENTtext/html
XML_CONTENTapplication/xml
TEXT_CONTENTtext/plain
CSS_CONTENTtext/css
JAVASCRIPT_CONTENTapplication/javascript
PDF_CONTENTapplication/pdf
ATOM_CONTENTapplication/atom+xml

HTTP Status Codes

Common status codes used with render():

CodeMeaning
200OK
201Created
204No Content
301Moved Permanently
302Found (Redirect)
400Bad Request
401Unauthorized
403Forbidden
404Not Found
406Not Acceptable
500Internal Server Error