PHP Classes

Crimson PHP HTTP Request Library: Handle HTTP requests with custom classes

Recommend this page to a friend!
  Info   View files Example   View files View files (9)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not enough user ratingsTotal: 52 This week: 3All time: 10,610 This week: 42Up
Version License PHP version Categories
crimson 1.0Custom (specified...5HTTP, PHP 5, PSR
Description 

Author

This package can be used to handle HTTP requests with custom classes.

It provides a HTTP server class based on the React PHP library that can listen to HTTP requests on a given IP address and port number

The server class dispatches the requests to a custom application handler class that can takes a HTTP request message as parameter and processes requests for a given URL pattern.

The application class can call a given handler class that generates the HTTP response headers and body by defining specific functions for that purpose.

Picture of Malik Naik
  Performance   Level  
Name: Malik Naik is available for providing paid consulting. Contact Malik Naik .
Classes: 9 packages by
Country: India India
Age: 25
All time rank: 3536233 in India India
Week rank: 51 Up4 in India India Up
Innovation award
Innovation award
Nominee: 5x

Example

<?php

require dirname(__DIR__) . '/vendor/autoload.php';

use
Crimson\RequestHandler;
use
Crimson\HttpServer;
use
Crimson\App;

/**
 * Every handler class must extend the Crimson\RequestHandler class and
 * override methods.
 */
class GetHandler extends RequestHandler {

  private
$msg;

 
/**
   * The first method to be called from the handler class. Use this method to do
   * initialization instead of using a constructor.
   */
 
public function initialize() {
   
$this->msg = $this->getClassArgs()['msg'];
  }

 
/**
   * Called before the request methods.
   */
 
public function prepare() {
  }

 
/**
   * Called when the request method is GET.
   */
 
public function get() {
   
$this->setStatus(200);
   
$this->setContentType('text/plain');
   
$this->write($this->msg);
  }

 
/**
   * Use this method to set the response headers
   */
 
public function setDefaultHeaders() {
   
$this->setHeader('Access-Control-Allow-Origin', '*');
  }

 
/**
   * This method is called at the end of the request.
   */
 
public function onFinish() {
  }
}

/**
 * The Crimson\App class takes only 1 argument that is an array.
 *
 * Each element of the App class argument should be an array with 3 elements:
 * 1. RegEx pattern representing the path to invoke the handler class methods.
 * 2. Name of the handler class.
 * 3. An array which will be passed to the constructor of the handler class
 * and can be accessed by `getClassArgs()` method.
 */
$app = new App([
  [
'\/foo', 'GetHandler', ['msg' => 'Hello, World!!']]
]);

/**
 * The HttpServer class takes 4 arguments:
 * 1. The instance of the App class(required).
 * 2. Array of TLS options.
 * 3. Address to access the Http server.
 * 4. The port on which the server listens to.
 */
$server = new HttpServer($app, [], '127.0.0.1', '8080');

// Start the server
$server->start();


Details

Crimson

A PHP Library to handle the HTTP requests in a clean way. This library is built on top of ReactPHP.

Installation

The `crimson` can be installed using composer via the following command:

composer require malik/crimson:^1.0.0

Usage

The following example code display the message 'Hello, World!!' when http://localhost:8080/foo or http://127.0.0.1:8080/foo is accessed with the GET request.

<?php

use Crimson\RequestHandler;
use Crimson\HttpServer;
use Crimson\App;

/
 * Every handler class must extend the Crimson\RequestHandler class and
 * override methods.
 */
class GetHandler extends RequestHandler {

  private $msg;

  /
   * The first method to be called from the handler class. Use this method to do
   * initialization instead of using a constructor.
   */
  public function initialize() {
    $this->msg = $this->getClassArgs()['msg'];
  }

  /
   * Called before the request methods.
   */
  public function prepare() {
  }

  /
   * Called when the request method is GET.
   */
  public function get() {
    $this->setStatus(200);
    $this->setContentType('text/plain');
    $this->write($this->msg);
  }

  /
   * Use this method to set the response headers
   */
  public function setDefaultHeaders() {
    $this->setHeader('Access-Control-Allow-Origin', '*');
  }

  /
   * This method is called at the end of the request.
   */
  public function onFinish() {
  }
}

/
 * The Crimson\App class takes only 1 argument that is an array.
 *
 * Each element of the App class argument should be an array with 3 elements:
 *   1. RegEx pattern representing the path to invoke the handler class methods.
 *   2. Name of the handler class.
 *   3. An array which will be passed to the constructor of the handler class
 *      and can be accessed by `getClassArgs()` method.
 */
$app = new App([
  ['\/foo', 'GetHandler', ['msg' => 'Hello, World!!']]
]);

/
 * The HttpServer class takes 4 arguments:
 *   1. The instance of the App class(required).
 *   2. Array of TLS options.
 *   3. Address to access the Http server.
 *   4. The port on which the server listens to.
 */
$server = new HttpServer($app, [], '127.0.0.1', '8080');

// Start the server
$server->start();

For more examples, check the examples/ directory in this repository.

License

GNU General Public License v2.0


  Files folder image Files  
File Role Description
Files folder imageexamples (3 files)
Files folder imagesrc (1 directory)
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file LICENSE.txt Lic. License
Accessible without login Plain text file README.md Doc. Read me

  Files folder image Files  /  examples  
File Role Description
  Accessible without login Plain text file get_request.php Example Example
  Plain text file groups.php Class Class source
  Plain text file redirect.php Class Class source

  Files folder image Files  /  src  
File Role Description
Files folder imageCrimson (3 files)

  Files folder image Files  /  src  /  Crimson  
File Role Description
  Plain text file App.php Class Class source
  Plain text file HttpServer.php Class Class source
  Plain text file RequestHandler.php Class Class source

 Version Control Unique User Downloads Download Rankings  
 100%
Total:52
This week:3
All time:10,610
This week:42Up