PHP code example of philharmony / http-factory

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

    

philharmony / http-factory example snippets


use Philharmony\Http\Factory\ServerRequestFactory;

$factory = new ServerRequestFactory();

$request = $factory->createServerRequest(
    'GET',
    'https://example.com'
);

use Philharmony\Http\Factory\RequestFactory;
use Philharmony\Http\Factory\StreamFactory;

$requestFactory = new RequestFactory();
$streamFactory = new StreamFactory();

$stream = $streamFactory->createStream('Hello, Philharmony');

$request = $requestFactory->createRequest('POST', 'https://example.com')
    ->withBody($stream);

use Philharmony\Http\Factory\ResponseFactory;

$response = (new ResponseFactory())->createResponse(200);

use Philharmony\Http\Factory\StreamFactory;

$streamFactory = new StreamFactory();

// From string
$stream = $streamFactory->createStream('Hello');

// From file
$stream = $streamFactory->createStreamFromFile('/path/to/file.txt');

// From resource
$stream = $streamFactory->createStreamFromResource(fopen('php://temp', 'r+'))

use Philharmony\Http\Factory\UploadedFileFactory;
use Philharmony\Http\Factory\StreamFactory;

$stream = (new StreamFactory())->createStream('file content');

// From stream
$uploadedFile = (new UploadedFileFactory())->createUploadedFile(
    $stream,
    $stream->getSize(),
    UPLOAD_ERR_OK,
    'file.txt',
    'text/plain'
);

// From file
$uploadedFileFromFile = (new UploadedFileFactory())->createUploadedFileFromFile(
    fileOrStream: '/path/to/file.txt',
    size: null, // used filesize
    errorStatus: UPLOAD_ERR_OK,
    clientFilename: 'file.txt',
    clientMediaType: 'text/plain',
    fullPath: '/path/to/file.txt' // PHP 8.1+ support
);

use Philharmony\Http\Factory\UriFactory;

$uri = (new UriFactory())->createUri('https://example.com/path');