PHP code example of michaelhall / http-client

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

    

michaelhall / http-client example snippets




ataTypes\Net\Url;
use MichaelHall\HttpClient\HttpClient;
use MichaelHall\HttpClient\HttpClientRequest;

$url = Url::parse('https://example.com/');
$client = new HttpClient();
$request = new HttpClientRequest($url);
$response = $client->send($request);

// Prints "success" if request was successful, "fail" otherwise.
echo $response->isSuccessful() ? 'success' : 'fail';

// Prints the response content.
echo $response->getContent();

// Prints the http code, e.g. 200.
echo $response->getHttpCode();

// Prints the headers.
foreach ($response->getHeaders() as $header) {
    echo $header;
}



ataTypes\Net\Url;
use DataTypes\System\FilePath;
use MichaelHall\HttpClient\HttpClientRequest;

$url = Url::parse('https://example.com/');

// Set the method.
$request = new HttpClientRequest($url, 'POST');

// Set a POST field.
$request->setPostField('Foo', 'Bar');

// Set a file.
$request->setFile('Baz', FilePath::parse('/path/to/file'));

// Add a header.
$request->addHeader('Content-type: application/json');

// Set raw content.
$request->setRawContent('{"Foo": "Bar"}');

// Client certificates.
$request->setCACertificate(FilePath::parse('/path/to/ca-certificate.pem'));
$request->setClientCertificate(FilePath::parse('/path/to/client-certificate.pem'));
$request->setClientKey(FilePath::parse('/path/to/client-key.pem'));



use DataTypes\Net\Url;
use MichaelHall\HttpClient\HttpClient;
use MichaelHall\HttpClient\HttpClientRequest;
use MichaelHall\HttpClient\HttpClientRequestInterface;
use MichaelHall\HttpClient\HttpClientResponse;
use MichaelHall\HttpClient\HttpClientResponseInterface;
use MichaelHall\HttpClient\RequestHandlers\RequestHandlerInterface;

tResponse(404);
    }
}

// Inject the custom request handler in constructor.
$client = new HttpClient(new FakeRequestHandler());

$request = new HttpClientRequest(Url::parse('https://example.com/foo'));
$response = $client->send($request);

// Prints "Hello World".
echo $response->getContent();

$request = new HttpClientRequest(Url::parse('https://example.com/'));
$response = $client->send($request);

// Prints "404".
echo $response->getHttpCode();