PHP code example of phppkg / http-message

1. Go to this page and download the library: Download phppkg/http-message library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

phppkg / http-message example snippets


use PhpPkg\Http\Message\Request;
use PhpPkg\Http\Message\Response;

$request = new Request($method, $uri);
$request = new ServerRequest(... ...);
$response = new Response($code);
... ...

use PhpPkg\Http\Message\HttpFactory;

$request = HttpFactory::createRequest($method, $uri);

// server request
$request = HttpFactory::createServerRequest('GET', 'http://www.abc.com/home');
$request = HttpFactory::createServerRequestFromArray($_SERVER);

$response = HttpFactory::createResponse($code);

use PhpPkg\Http\Message\Request;
use PhpPkg\Http\Message\Traits\ExtendedRequestTrait;

class MyRequest extends Request {
   use ExtendedRequestTrait; // 里面提供的更多方便使用的方法
}

// 

$request = new MyRequest(...);

$age = $request->getInt('age');
$name = $request->getTrimmed('name');

use PhpPkg\Http\Message\Response;
use PhpPkg\Http\Message\Traits\ExtendedResponseTrait;

class MyResponse extends Response {
   use ExtendedResponseTrait;
}