2017 © Pedro Peláez
 

library basic-http-client

A basic yet extensible HTTP client library providing different authentication methods written in PHP.

image

markenwerk/basic-http-client

A basic yet extensible HTTP client library providing different authentication methods written in PHP.

  • Friday, March 9, 2018
  • by bonscho
  • Repository
  • 1 Watchers
  • 1 Stars
  • 692 Installations
  • PHP
  • 5 Dependents
  • 0 Suggesters
  • 1 Forks
  • 1 Open issues
  • 22 Versions
  • 21 % Grown

The README.md

PHP Basic HTTP Client

Build Status SensioLabs Insight Code Climate Codacy Badge Latest Stable Version Total Downloads License, (*1)

A basic yet extensible HTTP client library providing different authentication methods written in PHP., (*2)

What about PSR-7?

PHP Basic HTTP Client is an alternative to other very good implementations like Guzzle that are following the PSR-7 guidelines., (*3)

This project not follows these guidelines for different reasons., (*4)

  1. PSR-7 is heavily over engineered due to also match complex edge cases.
  2. Objects implementing the PSR-7 interfaces have to be immutable wich is resulting in an unusual API from the PHP dev point of view and an unneccessarily increased need of performance.

Find out more at the „PSR-7 is imminent, and here's my issues with it“ blog post by PHP-FIG member Evert Pot and this discussion at Stackoverflow., (*5)

Installation

```{json} { "require": { "chroma-x/basic-http-client": "~4.0" } }, (*6)


## Usage ### Autoloading and namesapce ```{php} require_once('path/to/vendor/autoload.php');

Simple usage

Preparing the HTTP client

```{php} use ChromaX\BasicHttpClient; use ChromaX\BasicHttpClient\Request\Authentication; use ChromaX\BasicHttpClient\Request\Message;, (*7)

// Instantiating a basic HTTP client with the endpoints URL // If the endpoint uses the HTTPS schema a HttpsTransport instance will be used automatically. $client = new BasicHttpClient\BasicHttpClient('http://requestb.in/1aipzl31');, (*8)

// Adding an authentication method $client ->getRequest() ->addAuthentication(new Authentication\BasicAuthentication('username', 'password'));, (*9)

// Adding custom HTTP request headers and a session cookie $client ->getRequest() ->getMessage() ->addHeader(new Message\Header\Header('Content-Type', array('application/x-www-form-urlencoded'))) ->addHeader(new Message\Header\Header('Accept', array('text/html', 'text/*'))) ->addCookie(new Message\Cookie\Cookie('PHPSESSID', ''));, (*10)


#### Performing requests and read the response ##### Body-less requests (GET, HEAD and DELETE) Perfoming the following `GET` request with additional query parameters ```{php} $response = $client->get(array( 'paramName1' => 'paramValue1', 'paramName2' => 'paramValue2' ));

will result in the following HTTP request., (*11)

```{http} GET /1aipzl31?paramName1=paramValue1&paramName2=paramValue2 HTTP/1.1 Host: requestb.in Authorization: Basic dXNlcm5hbWU6cGFzc3dvcmQ= User-Agent: PHP Basic HTTP Client 1.0 Cookie: PHPSESSID= Content-Type: application/x-www-form-urlencoded Accept: text/html, text/*, (*12)


The same mechanic is offered to perform `HEAD` and `DELETE` requests wich all are body-less. ##### Body-full requests (POST, PUT, PATCH) Perfoming the following `POST` request with body data ```{php} $response = $client->post(array( 'paramName1' => 'paramValue1', 'paramName2' => 'paramValue2', 'paramName3' => array( 'key1' => 'value1', 'key2' => 'value2' ) ));

will result in the following HTTP request., (*13)

```{http} POST /1aipzl31?paramName1=paramValue1&paramName2=paramValue2 HTTP/1.1 Host: requestb.in Authorization: Basic dXNlcm5hbWU6cGFzc3dvcmQ= User-Agent: PHP Basic HTTP Client 1.0 Cookie: PHPSESSID= Content-Type: application/x-www-form-urlencoded Accept: text/html, text/* Content-Length: 101, (*14)

paramName1=paramValue1&paramName2=paramValue2&paramName3%5Bkey1%5D=value1&paramName3%5Bkey2%5D=value2, (*15)


The same mechanic is offered to perform `PUT` and `PATCH` requests wich all are body-full. --- ### Detailed usage The following example shows the usage with a more detailed configuration. #### Configuring an HTTP Transport instance ```{php} use ChromaX\BasicHttpClient\Request\Transport\HttpTransport; // Configuring a Transport instance $transport = new HttpTransport(); $transport ->setHttpVersion(HttpsTransport::HTTP_VERSION_1_1) ->setTimeout(5) ->setReuseConnection(true) ->setAllowCaching(true) ->setFollowRedirects(true) ->setMaxRedirects(10);

Configuring an HTTPS Transport instance

```{php} use ChromaX\BasicHttpClient\Request\Transport\HttpsTransport;, (*16)

// Configuring a Transport instance $transport = new HttpsTransport(); $transport ->setHttpVersion(HttpsTransport::HTTP_VERSION_1_1) ->setTimeout(5) ->setReuseConnection(true) ->setAllowCaching(true) ->setFollowRedirects(true) ->setMaxRedirects(10) ->setVerifyHost(true) ->setVerifyPeer(true);, (*17)


#### Configuring a Message instance with Body ```{php} use ChromaX\BasicHttpClient\Request\Message\Body\Body; use ChromaX\BasicHttpClient\Request\Message\Cookie\Cookie; use ChromaX\BasicHttpClient\Request\Message\Header\Header; use ChromaX\BasicHttpClient\Request\Message\Message; // Configuring a message Body instance $messageBody = new Body(); $messageBody->setBodyText( json_encode( array( 'paramName1' => 'paramValue1', 'paramName2' => 'paramValue2', 'paramName3' => array( 'key1' => 'value1', 'key2' => 'value2' ) ) ) ); // Configuring a Message instance $message = new Message(); $message ->addHeader(new Header('Content-Type', array('application/json'))) ->addHeader(new Header('Accept', array('application/json', 'text/*'))) ->addHeader(new Header('Runscope-Bucket-Auth', array('7a64dde7-74d5-4eed-b170-a2ab406eff08'))) ->addCookie(new Cookie('PHPSESSID', '<MY_SESSION_ID>')) ->setBody($messageBody);
Message and request Header instances

Please note, that headers have some unusual behaviours. Header names have an uniform way of nomenclature so the following three getter calls would have the same result., (*18)

```{php} $header1 = $message->getHeaderByName('Content-Type'); $header2 = $message->getHeaderByName('content-type'); $header3 = $message->getHeaderByName('CONTENT-Type');, (*19)


To allow multiple request headers using the same name, the method `addAdditionalHeader` provides such a logic. ```{php} // Add or replace a request header $message->addHeader(new Header('Custom-Header', array('CustomHeaderValue'))); // Add a request header and keep the existing one untouched $message->addAdditionalHeader(new Header('Custom-Header', array('AnotherCustomHeaderValue')));

Configuring an endpoints URL, build the Request instance and perform the HTTP request

For more information about the usage of the URL object please take a look at the PHP URL Util project., (*20)

```{php} use ChromaX\BasicHttpClient\Request\Authentication\BasicAuthentication; use ChromaX\BasicHttpClient\Request\Request; use ChromaX\UrlUtil\Url;, (*21)

// Setting up the endpoints URL $url = new Url('https://john:secret@yourapihere-com-98yq3775xff0.runscope.net:443/path/to/resource?arg1=123#fragment');, (*22)

// Configuring and performing a Request $request = new Request(); $request ->setUserAgent('PHP Basic HTTP Client Test 1.0') ->setUrl($url) ->addAuthentication(new BasicAuthentication('username', 'password')) ->setQueryParameters( array( 'paramName1' => 'paramValue1', 'paramName2' => 'paramValue2' ) ) ->setMethod(Request::REQUEST_METHOD_POST) ->setTransport($transport) ->setMessage($message) ->perform();, (*23)


The resulting HTTP request would be the following. ```{http} POST /?arg1=123#fragment HTTP/1.1 Host: yourapihere-com-98yq3775xff0.runscope.net Authorization: Basic dXNlcm5hbWU6cGFzc3dvcmQ= User-Agent: PHP Basic HTTP Client Test 1.0 Cookie: PHPSESSID=<MY_SESSION_ID> Content-Type: application/json Accept: application/json, text/* Runscope-Bucket-Auth: 7a64dde7-74d5-4eed-b170-a2ab406eff08 Custom-Header: CustomHeaderValue Custom-Header: AnotherCustomHeaderValue Content-Length: 102 {"paramName1":"paramValue1","paramName2":"paramValue2","paramName3":{"key1":"value1","key2":"value2"}}

Usage of authentication methods

You can add one or more Authentication instances to every Request instance. At the moment this project provides classes for HTTP Basic Authentication and SSL Client Certificate Authentication., (*24)

HTTP Basic Authentication

Required credentials are a username and a password that get provided to the class constructor as arguments., (*25)

```{php} use ChromaX\BasicHttpClient\Request\Authentication\BasicAuthentication; use ChromaX\BasicHttpClient\Request\Request;, (*26)

// Configuring the authentication $basicAuthentication = new BasicAuthentication('username', 'password');, (*27)

// Adding the authentication instance to the Request $request = new Request(); $request->addAuthentication($basicAuthentication);, (*28)


#### SSL Client Certificate Authentication Required credentials are a *Certificate Authority Certificate*, a *Client Certificate* and the password that is used to decrypt the Client Certificate that get provided to the class constructor as arguments. ```{php} use ChromaX\BasicHttpClient\Request\Authentication\ClientCertificateAuthentication; use ChromaX\BasicHttpClient\Request\Request; // Configuring the authentication $clientCertificateAuthentication = new ClientCertificateAuthentication( '/var/www/project/clientCert/ca.crt', '/var/www/project/clientCert/client.crt', 'clientCertPassword' ); // Adding the authentication instance to the Request $request = new Request(); $request->addAuthentication($clientCertificateAuthentication);

Reading from the resulting Response object

Getting the response object

If using the BasicHttpClient the response object is returned by the termination methods listed above. If directly using the Request instance, you can get the Response object via a getter., (*29)

```{php} // Getting the response ChromaX\BasicHttpClient\Response\Response object $response = $request->getResponse();, (*30)

// Reading the HTTP status code as integer; will return 200 echo print_r($response->getStatusCode(), true) . PHP_EOL;, (*31)

// Reading the HTTP status text as string; will return HTTP/1.1 200 OK echo print_r($response->getStatusText(), true) . PHP_EOL;, (*32)

// Reading the HTTP response headers as array of ChromaX\BasicHttpClient\Response\Header\Header objects echo print_r($response->getHeaders(), true) . PHP_EOL;, (*33)

// Reading the HTTP response body as string echo print_r($response->getBody(), true) . PHP_EOL;, (*34)


--- ## Getting effective Request information After successful performing the request, the effective request information is tracked back to the Request object. They can get accessed as follows. ```{php} // Getting the effective endpoint URL including the query parameters echo print_r($request->getEffectiveEndpoint(), true) . PHP_EOL; // Getting the effective HTTP status, f.e. `POST /?paramName1=paramValue1&paramName2=paramValue2&paramName3=1&paramName4=42 HTTP/1.1` echo print_r($request->getEffectiveStatus(), true) . PHP_EOL; // Getting the effective raw request headers as string echo print_r($request->getEffectiveRawHeader(), true) . PHP_EOL; // Getting the effective request headers as array of `ChromaX\BasicHttpClient\Request\Message\Header\Header` objects echo print_r($request->getEffectiveHeaders(), true) . PHP_EOL.PHP_EOL;

Getting some transactional statistics

```{php} // Getting the statistics ChromaX\BasicHttpClient\Response\Statistics\Statistics object $statistics = $request->getResponse()->getStatistics();, (*35)

// Reading the redirection URL if the server responds with an redirect HTTP header and // followRedirects is set to false echo print_r($statistics->getRedirectEndpoint(), true).PHP_EOL;, (*36)

// Reading the numbers of redirection as integer echo print_r($statistics->getRedirectCount(), true).PHP_EOL;, (*37)

// Getting the time in seconds the redirect utilized as float echo print_r($statistics->getRedirectTime(), true).PHP_EOL;, (*38)

// Getting the time in seconds that was utilized until the connection was established echo print_r($statistics->getConnectionEstablishTime(), true).PHP_EOL;, (*39)

// Getting the time in seconds that was utilized until the DNS hostname lookup was done echo print_r($statistics->getHostLookupTime(), true).PHP_EOL;, (*40)

// Getting the time in seconds that was utilized before the first data was sent echo print_r($statistics->getPreTransferTime(), true).PHP_EOL;, (*41)

// Getting the time in seconds that was utilized before the first data was received echo print_r($statistics->getStartTransferTime(), true).PHP_EOL;, (*42)

// Getting the time in seconds that was utilized to perfom the request an read the response echo print_r($statistics->getTotalTime(), true).PHP_EOL; ```, (*43)


Extending the Basic HTTP Client

Every part of the client is based upon proper interfaces. Most class instances can get injected into the client itself. If you want to extend the client just write some classes implementing the according interface and you´re done with that., (*44)

Take a look at the PHP JSON HTTP Client which is an extension of the PHP Basic HTTP Client., (*45)


Exception handling

PHP Basic HTTP Client provides different exceptions – also provided by the PHP Common Exceptions project – for proper handling.
You can find more information about PHP Common Exceptions at Github., (*46)

Exceptions to be expected

In general you should expect that any setter method could thrown an \InvalidArgumentException. The following exceptions could get thrown while using PHP Basic HTTP Client., (*47)

  • ChromaX\CommonException\IoException\FileNotFoundException on configuring a ClientCertificateAuthenticationinstance
  • ChromaX\CommonException\IoException\FileReadableException on configuring a ClientCertificateAuthenticationinstance
  • ChromaX\BasicHttpClient\Exception\HttpRequestAuthenticationException on performing a request
  • ChromaX\BasicHttpClient\Exception\HttpRequestException on performing a request
  • ChromaX\CommonException\NetworkException\ConnectionTimeoutException on performing a request
  • ChromaX\CommonException\NetworkException\CurlException on performing a request

Contribution

Contributing to our projects is always very appreciated.
But: please follow the contribution guidelines written down in the CONTRIBUTING.md document., (*48)

License

PHP Basic HTTP Client is under the MIT license., (*49)

The Versions

09/03 2018

dev-master

9999999-dev http://markenwerk.net/

A basic yet extensible HTTP client library providing different authentication methods written in PHP.

  Sources   Download

MIT

The Requires

 

The Development Requires

json restful basic auth ssl http client client certificate auth

09/03 2018

3.3.7

3.3.7.0 http://markenwerk.net/

A basic yet extensible HTTP client library providing different authentication methods written in PHP.

  Sources   Download

MIT

The Requires

 

The Development Requires

json restful basic auth ssl http client client certificate auth

09/03 2018

3.3.6

3.3.6.0 http://markenwerk.net/

A basic yet extensible HTTP client library providing different authentication methods written in PHP.

  Sources   Download

MIT

The Requires

 

The Development Requires

json restful basic auth ssl http client client certificate auth

17/08 2017

3.3.5

3.3.5.0 http://markenwerk.net/

A basic yet extensible HTTP client library providing different authentication methods written in PHP.

  Sources   Download

MIT

The Requires

 

The Development Requires

json restful basic auth ssl http client client certificate auth

18/07 2016

3.3.4

3.3.4.0 http://markenwerk.net/

A basic yet extensible HTTP client library providing different authentication methods written in PHP.

  Sources   Download

MIT

The Requires

 

The Development Requires

json restful basic auth ssl http client client certificate auth

14/07 2016

3.3.3

3.3.3.0 http://markenwerk.net/

A basic yet extensible HTTP client library providing different authentication methods written in PHP.

  Sources   Download

MIT

The Requires

 

The Development Requires

json restful basic auth ssl http client client certificate auth

14/07 2016

3.3.2

3.3.2.0 http://markenwerk.net/

A basic yet extensible HTTP client library providing different authentication methods written in PHP.

  Sources   Download

MIT

The Requires

 

The Development Requires

json restful basic auth ssl http client client certificate auth

14/07 2016

3.0.3

3.0.3.0 http://markenwerk.net/

A basic yet extensible HTTP client library providing different authentication methods written in PHP.

  Sources   Download

MIT

The Requires

 

The Development Requires

json restful basic auth ssl http client client certificate auth

14/07 2016

3.03

3.03.0.0 http://markenwerk.net/

A basic yet extensible HTTP client library providing different authentication methods written in PHP.

  Sources   Download

MIT

The Requires

 

The Development Requires

json restful basic auth ssl http client client certificate auth

14/07 2016

3.3.1

3.3.1.0 http://markenwerk.net/

A basic yet extensible HTTP client library providing different authentication methods written in PHP.

  Sources   Download

MIT

The Requires

 

The Development Requires

json restful basic auth ssl http client client certificate auth

14/07 2016

3.0.2

3.0.2.0 http://markenwerk.net/

A basic yet extensible HTTP client library providing different authentication methods written in PHP.

  Sources   Download

MIT

The Requires

 

The Development Requires

json restful basic auth ssl http client client certificate auth

11/07 2016

3.0.0

3.0.0.0 http://markenwerk.net/

A basic yet extensible HTTP client library providing different authentication methods written in PHP.

  Sources   Download

MIT

The Requires

 

The Development Requires

json restful basic auth ssl http client client certificate auth

11/07 2016

3.0.1

3.0.1.0 http://markenwerk.net/

A basic yet extensible HTTP client library providing different authentication methods written in PHP.

  Sources   Download

MIT

The Requires

 

The Development Requires

json restful basic auth ssl http client client certificate auth

03/05 2016

2.0.1

2.0.1.0 http://markenwerk.net/

A basic yet extensible HTTP client library providing different authentication methods written in PHP.

  Sources   Download

MIT

The Requires

 

The Development Requires

json restful basic auth ssl http client client certificate auth

02/05 2016

2.0.0

2.0.0.0 http://markenwerk.net/

A basic yet extensible HTTP client library providing different authentication methods written in PHP.

  Sources   Download

MIT

The Requires

 

The Development Requires

json restful basic auth ssl http client client certificate auth

29/04 2016

1.0.0

1.0.0.0 http://markenwerk.net/

A basic yet extensible HTTP client library providing different authentication methods written in PHP.

  Sources   Download

MIT

The Requires

 

The Development Requires

json restful basic auth ssl http client client certificate auth

28/04 2016

0.2.0

0.2.0.0 http://markenwerk.net/

A basic yet extensible HTTP client library providing different authentication methods written in PHP.

  Sources   Download

MIT

The Requires

 

The Development Requires

json restful basic auth ssl http client client certificate auth

28/04 2016

0.1.0-alpha.3

0.1.0.0-alpha3 http://markenwerk.net/

A basic yet extensible HTTP client library providing different authentication methods written in PHP.

  Sources   Download

MIT

The Requires

 

The Development Requires

json restful basic auth ssl http client client certificate auth

27/04 2016

0.1.0-alpha.2

0.1.0.0-alpha2 http://markenwerk.net/

A basic yet extensible HTTP client library providing different authentication methods written in PHP.

  Sources   Download

MIT

The Requires

 

The Development Requires

json restful basic auth ssl http client client certificate auth

27/04 2016

0.0.1

0.0.1.0 http://markenwerk.net/

A PHP HTTP client library supporting different protocols like RESTful JSON, JSON-RPC, XML-RPC and SOAP.

  Sources   Download

MIT

The Requires

 

The Development Requires

json restful soap json-rpc ssl xml-rpc http client

27/04 2016

0.0.1-alpha

0.0.1.0-alpha http://markenwerk.net/

A PHP HTTP client library supporting different protocols like RESTful JSON, JSON-RPC, XML-RPC and SOAP.

  Sources   Download

MIT

The Requires

 

The Development Requires

json restful soap json-rpc ssl xml-rpc http client

27/04 2016

0.1.0-alpha.1

0.1.0.0-alpha1 http://markenwerk.net/

A PHP HTTP client library supporting different protocols like RESTful JSON, JSON-RPC, XML-RPC and SOAP.

  Sources   Download

MIT

The Requires

 

The Development Requires

json restful soap json-rpc ssl xml-rpc http client