PHP code example of php-pico / http-factory

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

    

php-pico / http-factory example snippets


use PhpPico\Http\Factory\HttpFactory;

$factory = new HttpFactory();

$request  = $factory->createRequest('GET', 'https://example.com');
$response = $factory->createResponse(200);
$uri      = $factory->createUri('https://example.com/path?q=1');

$stream = $factory->createStream('hello');
$stream = $factory->createStreamFromFile('/path/to/file.txt', 'r');
$stream = $factory->createStreamFromResource(fopen('php://memory', 'r+'));

$file = $factory->createUploadedFile($stream, size: 5, clientFilename: 'hello.txt');

use PhpPico\Http\Factory\HttpFactory;
use PhpPico\Http\Factory\ServerRequestCreator;

$creator = new ServerRequestCreator(new HttpFactory());

$request = $creator->fromGlobals();

$request = $creator->fromArrays(
    server: $_SERVER,
    get: $_GET,
    post: $_POST,
    cookie: $_COOKIE,
    files: $_FILES,
);

use PhpPico\Http\Factory\RequestFactoryTrait;
use PhpPico\Http\Factory\ResponseFactoryTrait;
use Psr\Http\Message\RequestFactoryInterface;
use Psr\Http\Message\ResponseFactoryInterface;

final class MyFactory implements RequestFactoryInterface, ResponseFactoryInterface
{
    use RequestFactoryTrait;
    use ResponseFactoryTrait;
}

$creator = new ServerRequestCreator(new HttpFactory(), ['10.0.0.0/8']);

$request = $creator->fromGlobals();