Express.php is a new HTTP - Server especially made for RESTful APIs written in PHP.

Related tags

HTTP expressphp
Overview

express.php

Express.php is a new HTTP - Server especially made for RESTful APIs written in PHP.

Features

Fast

The Library is handles requests fast and precise, it consumes around 20ms per request.

Easy

This project comes up with an easy to use syntax, that similar to express.js

Get started

Installation

Download from github, and drop in your target project.

Docs

The basic application

listen(80); ">


use express\App;
use express\io\Request;
use express\io\Response;

require "src/express/App.php";

$app = new App();

# All routes have to be set here. After listen, no code will be executed

$app->listen(80);

Add a Route

To add a basic GET route, you can use the following code:



use express\io\Request;
use express\io\Response;

$app->get("/", function (Request $request, Response $response) {
	# Code that should be executed once a request received
});
  • For a POST route, use $app->post()
  • For a PUT route, use $app->put()
  • For a PATCH route, use $app->patch()
  • For a DELETE route, use $app->delete()

Query parameters

HTTP Queries are the part after the ? in an URL. This library comes up with a simple API for handling it: When registering the route, you can simply add (for example) ?name=username, the request now requires a query parameter with the name name. A request for this would look something like localhost/users?name=dummyname. In the code, this means that there is a new property called like the query parameter name, username in this case (The value you defined when setting up the route). It can simply be accessed using $request->name

username; }); $app->get("/messages?id=message_id", function (Request $request, Response $response) { echo $request->message_id; }); ">


use express\io\Request;
use express\io\Response;

$app->get("/users?name=username", function (Request $request, Response $response) {
	echo $request->username;
});

$app->get("/messages?id=message_id", function (Request $request, Response $response) {
	echo $request->message_id;
});

Route parameters

Route parameters are custom parameters that can contain a client - defined value (matching a pattern, if set). This can be done by using #name, name will be the property name accessible in the request. This can contain any value #message_id, ...

userId; }); ">


use express\io\Request;
use express\io\Response;

$app->post("/users/#userId/nick", function (Request $request, Response $response) {
	echo $request->userId;
});

$request->userId can now contain nearly any value. To prevent this, we can use patterns:

Route Parameter Patterns

Patterns are used to specify which values a parameter can contain. There are multiple settings to use, but in general, patterns are JSON encoded arrays, set to the route



use express\io\Request;
use express\io\Response;

$app->get('/users/{"name": "userId", "len-min": 16, "len-max": 16}', function (Request $request, Response $response): void {
	
});

Now, that you know how its basically built, lets speak of the possible settings you are able to set:

Name Description Values Required
name Sets the name of the parameter Any string (Without /) ✔️
type Sets the required of the parameter string/int/float
len-min Sets the minimum length of the parameter any number (smaller than len-max)
len-max Sets the maximum length of the parameter any number (bigger than len-min)

Redirect requests



use express\io\Request;
use express\io\Response;

$app->get('/users', function (Request $request, Response $response): void {
	$response->redirect("/members");
});

Set a response body

You can set a response code by using $response->body(). This can either be a string, or an array that will get JSON encoded once the request is sent. String:



use express\io\Request;
use express\io\Response;

$app->get('/users', function (Request $request, Response $response): void {
	$response->body("Access denied");
});

Array: (For JSON encoding)

"Access denied"]); }); ">


use express\io\Request;
use express\io\Response;

$app->get('/users', function (Request $request, Response $response): void {
	$response->body(["error" => "Access denied"]);
});

Response Codes

You can set a response code for the response using $response->code(). All types there you can find all supported status codes with their names in express\utils\StatusCodes.

"Access denied"]); }); ">


use express\io\Request;
use express\io\Response;

$app->get('/users', function (Request $request, Response $response): void {
	$response->code(403);
	$response->body(["error" => "Access denied"]);
});

If your response code is not supported yet, use $response->customResponseCodeMessage().

Respond as a HTML



use express\io\Request;
use express\io\Response;

$app->get('/users', function (Request $request, Response $response): void {
	$response->html("/members");
});

Change the HTTP Content-Type



use express\io\Request;
use express\io\Response;

$app->get('/', function (Request $request, Response $response): void {
	$response->contentType("text/plain");
});

You can check this page for a basic list of common content types.

You might also like...
HTTP header kit for PHP 7.1+ (incl. PHP 8) based on PSR-7

HTTP header kit for PHP 7.1+ (incl. PHP 8) based on PSR-7 Installation composer require sunrise/http-header-kit How to use? HTTP Header Collection Mor

Guzzle, an extensible PHP HTTP client
Guzzle, an extensible PHP HTTP client

Guzzle, PHP HTTP client Guzzle is a PHP HTTP client that makes it easy to send HTTP requests and trivial to integrate with web services. Simple interf

Requests for PHP is a humble HTTP request library. It simplifies how you interact with other sites and takes away all your worries.

Requests for PHP Requests is a HTTP library written in PHP, for human beings. It is roughly based on the API from the excellent Requests Python librar

A Chainable, REST Friendly, PHP HTTP Client. A sane alternative to cURL.

Httpful Httpful is a simple Http Client library for PHP 7.2+. There is an emphasis of readability, simplicity, and flexibility – basically provide the

PHP's lightweight HTTP client

Buzz - Scripted HTTP browser Buzz is a lightweight (1000 lines of code) PHP 7.1 library for issuing HTTP requests. The library includes three clients

HTTPlug, the HTTP client abstraction for PHP

HTTPlug HTTPlug, the HTTP client abstraction for PHP. Intro HTTP client standard built on PSR-7 HTTP messages. The HTTPlug client interface is compati

Requests for PHP is a humble HTTP request library. It simplifies how you interact with other sites and takes away all your worries.

Requests for PHP Requests is a HTTP library written in PHP, for human beings. It is roughly based on the API from the excellent Requests Python librar

Unirest in PHP: Simplified, lightweight HTTP client library.

Unirest for PHP Unirest is a set of lightweight HTTP libraries available in multiple languages, built and maintained by Mashape, who also maintain the

A simple PHP Toolkit to parallel generate combinations, save and use the generated terms to brute force attack via the http protocol.
A simple PHP Toolkit to parallel generate combinations, save and use the generated terms to brute force attack via the http protocol.

Brutal A simple PHP Toolkit to parallel generate combinations, save and use the generated terms to apply brute force attack via the http protocol. Bru

Owner
I'm developing stuff in PHP / JS / Java
null
🐼 Framework agnostic package using asynchronous HTTP requests and PHP generators to load paginated items of JSON APIs into Laravel lazy collections.

Framework agnostic package using asynchronous HTTP requests and generators to load paginated items of JSON APIs into Laravel lazy collections.

Andrea Marco Sartori 61 Dec 3, 2022
A simple script i made that generate a valid http(s) proxy in json format with its geo-location info

Gev Proxy Generator GPG is a simple PHP script that generate a proxy using free services on the web, the proxy is HTTP(s) and it generate it in json f

gev 1 Nov 15, 2021
Declarative HTTP Clients using Guzzle HTTP Library and PHP 8 Attributes

Waffler How to install? $ composer require waffler/waffler This package requires PHP 8 or above. How to test? $ composer phpunit Quick start For our e

Waffler 3 Aug 26, 2022
Requests - a HTTP library written in PHP, for human beings

Requests is a HTTP library written in PHP, for human beings. It is roughly based on the API from the excellent Requests Python library. Requests is ISC Licensed (similar to the new BSD license) and has no dependencies, except for PHP 5.6+.

WordPress 3.5k Jan 6, 2023
LittleProxy is a high performance HTTP proxy written in Java atop Trustin Lee's excellent Netty event-based networking library

LittleProxy is a high performance HTTP proxy written in Java atop Trustin Lee's excellent Netty event-based networking library

null 1.9k Dec 26, 2022
↪️ Bypass for PHP creates a custom HTTP Server to return predefined responses to client requests

Bypass for PHP provides a quick way to create a custom HTTP Server to return predefined responses to client requests.Useful for tests with Pest PHP or PHPUnit.

CiaReis 101 Dec 1, 2022
Pushpin is a publish-subscribe server, supporting HTTP and WebSocket connections.

Pushpin and 1 million connections Pushpin is a publish-subscribe server, supporting HTTP and WebSocket connections. This repository contains instructi

Fanout 14 Jul 15, 2022
Event-driven, streaming HTTP client and server implementation for ReactPHP

HTTP Event-driven, streaming HTTP client and server implementation for ReactPHP. This HTTP library provides re-usable implementations for an HTTP clie

ReactPHP 640 Dec 29, 2022
A WebSocket server written in PHP.

WebSocketServer A WebSocket server written in PHP. If you like this project gift us a ⭐ . Installation. $ composer require thenlabs/websocket-server 1

ThenLabs 3 Nov 28, 2022
Read-only WebDAV server written in php8.0; supports browsing archives and GETting files in encodings other than what's on disk

Best Read-only WebDAV Server: TODO Features and notes of implementation Keeping generated files in a place that nginx can find them (2 ways to do this

Joe Koop 1 Nov 17, 2021