PHP code example of kemist / http

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

    

kemist / http example snippets



$request=new Kemist\Http\Request('http://httpbin.org/get?sd=43','GET',array('accept'=>'text/html','connection'=>'close'));

// cURL client
$client=new Kemist\Http\Client\CurlClient();
// OR Socket-based client
$client=new Kemist\Http\Client\SocketClient();

$response=$client->send($request);

var_dump($response->getHeaders());
var_dump($response->getBody()->getContents());


$request=(new Kemist\Http\Request())
        ->withUri(new Kemist\Http\Uri('http://httpbin.org/post'))
        ->withMethod('POST')
        ->withHeader('accept','text/html')
        ->withHeader('connection','close')
        ->withBody(new Kemist\Http\Stream\StringStream('param1=value1&param2=value2'))
;

// cURL client
$client=new Kemist\Http\Client\CurlClient();
// OR Socket-based client
$client=new Kemist\Http\Client\SocketClient();

$response=$client->send($request);

var_dump($response->getHeaders());
var_dump(json_decode($response->getBody()));



// Client options and their default values
$options=array(
        'follow_redirections' => true,
        'max_redirections' => 10,
        'use_cookies' => true,
        'dechunk_content' => true,
        'decode_content' => true,
        'connection_timeout' => 30,
        'timeout' => 60,
    );
// Options are set through the client constructor
$client=new Kemist\Http\Client\SocketClient($options);


$server = new Kemist\Http\Server\Server();
// Appends a closure middleware
$server->appendMiddleware(function($request, $response, $server) {    
    $response->getBody()->write('world!');    
    return $response;
});
// Prepends a closure middleware
$server->prependMiddleware(function($request, $response, $server) {    
    $response->getBody()->write('Hello ');    
    return $response;
});

$response=$server->handle();
echo $response;



$server->appendMiddleware(function($request, $response, $server) {    
    $response->getBody()->write('This string never appears!');    
    return $response;
});

$server->prependMiddleware(function($request, $response, $server) {    
    $response->getBody()->write('Hello world!');    
    $server->stopPropagation();
    return $response;
});


$server->appendMiddleware(new Kemist\Http\Server\IsNotModifiedMiddleware());