PHP code example of effectra / http-message

1. Go to this page and download the library: Download effectra/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/ */

    

effectra / http-message example snippets


use Effectra\Http\Message\Request;
use Effectra\Http\Message\Uri;

$uri = new Uri('https://api.example.com/users');
$request = new Request('POST', $uri, ['Content-Type' => 'application/json'], '{"name":"John"}');

echo $request->getMethod();                 // POST
echo $request->getHeaderLine('Content-Type'); // application/json

$new = $request->withMethod('PUT')->withUri(new Uri('https://other.com'));
// $request is unchanged

$request->setMethod('PATCH')->setUri(new Uri('/local'));
// $request is modified in place

use Effectra\Http\Message\Response;

$response = new Response(200, ['X-Custom' => 'value'], 'OK');
echo $response->getStatusCode();              // 200
echo $response->getReasonPhrase();            // OK

// JSON response
$res = Response::json(['status' => 'ok']);
$res = (new Response())->withJson(['error' => 'bad'], 400);

// HTML response
$res = Response::html('<h1>Hello</h1>', 200);

// Empty response (e.g. 204 No Content)
$res = Response::emptyResponse(204);

Response::HTTP_OK;               // 200
Response::HTTP_NOT_FOUND;        // 404
Response::HTTP_I_AM_A_TEAPOT;    // 418

use Effectra\Http\Message\ServerRequest;

$request = ServerRequest::fromGlobals();

echo $request->getMethod();          // GET, POST, etc.
echo $request->getServerParams()['REMOTE_ADDR'];
$input = $request->getParsedBody();  // POST / JSON body

use Effectra\Http\Message\Uri;

$uri = new Uri('https://example.com/path?q=hello#section');

echo $uri->getScheme();    // https
echo $uri->getHost();      // example.com
echo $uri->getPath();      // /path
echo $uri->getQuery();     // q=hello

$new = $uri->withScheme('http')->withHost('other.org');

use Effectra\Http\Message\Stream;

// From a string
$stream = new Stream('content');
echo $stream->getContents(); // content

// From a file path (opens automatically)
$stream = new Stream('/path/to/file.txt');

// From a resource
$stream = new Stream(fopen('php://memory', 'r+'));
$stream->write('data');

use Effectra\Http\Message\UploadedFile;

$file = new UploadedFile(
    $_FILES['avatar']['tmp_name'],
    $_FILES['avatar']['size'],
    $_FILES['avatar']['error'],
    $_FILES['avatar']['name'],
    $_FILES['avatar']['type']
);

$stream = $file->getStream();            // read file contents
echo $file->getClientFilename();         // original name
$file->moveTo('/storage/avatars/1.jpg');

$request = new Request('GET', '/', ['Content-Type' => 'text/html']);

$request->hasHeader('content-type');     // true
$request->hasHeader('CONTENT-TYPE');     // true
$request->hasHeader('Content-Type');     // true

$request->getHeader('CONTENT-TYPE');     // ['text/html']

$cloned = $request->withoutHeader('content-type');
$cloned = $request->withAddedHeader('X-Custom', 'val');

use Effectra\Http\Message\Attribute\Immutable;

#[Immutable('This class follows the PSR-7 immutable pattern')]
class MyMessage
{
    // ...
}