PHP code example of koded / http

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

    

koded / http example snippets


class ServerRequest extends ClientRequest implements Request {}

class ClientRequest implements RequestInterface, JsonSerializable {}

class ServerResponse implements Response, JsonSerializable {}



use Koded\Http\HttpFactory;

$httpFactory = new HttpFactory;

$clientRequest = $httpFactory->createRequest('GET', '/');
$serverRequest = $httpFactory->createServerRequest('GET', '/');

$response = $httpFactory->createResponse(201);

$stream = $httpFactory->createStream('Hello there');
$stream = $httpFactory->createStreamFromFile('file.name', '+w');
$stream = $httpFactory->createStreamFromResource($resource);

$uri = $httpFactory->createUri('/');

$uploadedFile = $httpFactory->createUploadedFile($stream);



use Koded\Http\Client\ClientFactory;

$http = new ClientFactory(ClientFactory::CURL); // or ClientFactory::PHP

$http->get('/', $headers);
$http->post('/', $body, $headers);
$http->put('/', $body, $headers);
$http->patch('/', $body, $headers);
$http->delete('/', $headers);
$http->head('/', $headers);



use Koded\Http\Client\ClientFactory;
use Koded\Http\ClientRequest;

$request = new ClientRequest('POST', 'https://...', ['foo' => 'bar']);
$response = (new ClientFactory)->sendRequest($request);

class FormValidator implements HttpInputValidator {

    public function validate(Data $input): array {
        if (empty($input->get('username'))) {
            return ['message' => 'Username is