2017 © Pedro Peláez
 

library php-http-client

An easy to use wrapper for the curl http library in PHP

image

rehyved/php-http-client

An easy to use wrapper for the curl http library in PHP

  • Saturday, June 23, 2018
  • by mpwaldhorst
  • Repository
  • 2 Watchers
  • 1 Stars
  • 180 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 7 Versions
  • 21 % Grown

The README.md

HTTP Client for PHP

A HTTP Client implementation on top of the PHP cURL extension for PHP inspired by the builder pattern., (*1)

Build Status, (*2)

This library uses a builder pattern similar to the Apache HTTP client Fluent API for Java. The goal is to provide a readable and maintainable way of writing HTTP request logic inside your PHP websites and applications., (*3)

Feedback is always welcome., (*4)

Installation

Prerequisites: * PHP 7.0 or higher * PHP cURL module (ext-curl) * PHP DOM module (ext-dom), (*5)

This library is available through Packagist and can be imported using Composer:, (*6)

composer require rehyved/php-http-client

Usage

The goal with this library is to make it easy to produce HTTP requests in PHP whilst keeping the code readable and understandable. The main starting point for this is the HttpRequest class and the HttpResponse class., (*7)

HttpRequest class

The following examples show different usages of the HttpRequest class to perform HTTP requests:, (*8)

Overriding default configuration

Default configuration for some settings can be configured globally to prevent having to provide these values on each creation of an HttpRequest., (*9)

The following configuration options are available by defining the appropriate constants with define():, (*10)

  • RPHC_DEFAULT_HEADERS - An associative array of header name-> header value to be included with each HTTP request (default: array())
  • RPHC_DEFAULT_TIMEOUT - An int value indicating the number of seconds to use as a timeout for HTTP requests (default: 30)
  • RPHC_DEFAULT_VERIFY_SSL_CERTIFICATE - a boolean value indicating if the validity of SSL certificates should be enforced in HTTP requests (default: true)

Request types

GET request
$response = HttpRequest::create("https://httpbin.org")  // Base url
    ->get("get");                                       // Path

https://httpbin.org is a nice service to test HTTP requests against, it provides several ways to try different kinds of requests with a configurable response, (*11)

PUT request
$response = HttpRequest::create("https://httpbin.org")  // Base url
    ->contentType("application/json")                   // Content-Type header
->put("put", array("key" => "value");                   // Path & body
POST request
$response = HttpRequest::create("https://httpbin.org")  // Base url
    ->contentType("application/json")                   // Content-Type header
->post("post", array("key" => "value");                 // Path & body
DELETE request
$response = HttpRequest::create("https://httpbin.org")  // Base url
    ->contentType("application/json")                   // Content-Type header
->delete("delete", array("key" => "value");               // Path & body

Adding query parameters

$response = HttpRequest::create("https://httpbin.org")  // Base url
    ->parameter("search", "Search query")               // Add a single query parameter
    ->parameters(array("key" => "value"))               // Add an array of query parameters
    ->get("get");                                       // Path

Adding Headers

$response = HttpRequest::create("https://httpbin.org")  // Base url
    ->header("Accept", "application/json")              // Add a single header
    ->headers(array("key" => "value"))                  // Add an array of headers
    ->get("get");                                       // Path

Adding Cookies

$response = HttpRequest::create("https://httpbin.org")  // Base url
    ->cookie("search", "Search query")                  // Add a single cookie
    ->cookies(array("key" => "value"))                  // Add an array of cookies
    ->cookies()                                         // Adds all cookies from $_COOKIE to the request
    ->get("get");                                       // Path

Basic Authentication

$response = HttpRequest::create("https://httpbin.org")  // Base url
    ->basicAuthentication("username", "password")       // Adds basic authentication to the request
    ->get("get");                                       // Path

Authorization header

$response = HttpRequest::create("https://httpbin.org")  // Base url
    ->authorization("Bearer", "<JWT-token>")            // Convenience method to add an Authorization header
    ->get("get");                                       // Path

Changing request timeout

$response = HttpRequest::create("https://httpbin.org")  // Base url
    ->timeout(20)                                       // Changes the timeout for the request to 20 seconds
    ->get("get");                                       // Path

Disabling SSL certificate verification

NOTE: This feature is not recommended in a production system but is meant as a convenience option in test environments, (*12)

$response = HttpRequest::create("https://httpbin.org")  // Base url
    ->verifySslCertificate(false)                       // Disables the verification of SSL certificates
    ->get("get");                                       // Path

HttpResponse class

The $response variable will hold an instance of HttpResponse. This type of object holds the resulting content of the HttpRequest and provides useful methods to extract further information., (*13)

Status handling

$isError = $response->isError()){ // checks the HTTP status to see if it is an error see the HttpStatus class
$statusCode = $response->getHttpStatus(); 

Header handling

$contentType = $response->getContentType();
$header = $response->getHeader("Content-Type");
$headers = $response->getHeaders(); // an associative array of header name -> header value
$cookie = $response->getCookie("chocolatechip"); // returns a HttpCookie object
$cookie = $response->getCookies(); // a list of HttpCookie objects
$response->importCookies(); // Adds all cookies to the current session by using setcookie (http://php.net/manual/en/function.setcookie.php)

Response body handling

$contentLength = $response->getContentLength();
$content = $response->getContent(); // Will deserialize JSON or XML content if the matching Content-Type was received 
$contentRaw = $response->getContentRaw() // Does not try to deserialize and returns the raw response body

HttpStatus class

This class provides constants to retrieve the matching status code for an HTTP status as well as convenience methods to get the reason phrase and check the type of a status code., (*14)

Constants

The class provides constants for the HTTP statuses, for example:, (*15)

HttpStatus::OK
HttpStatus::CLIENT_ERROR
HttpStatus::SERVER_ERROR

etc...

Methods

HttpStatus::isInformational(int $statusCode)
HttpStatus::isSuccessful(int $statusCode)
HttpStatus::isRedirection(int $statusCode)
HttpStatus::isClientError(int $statusCode)
HttpStatus::isServerError(int $statusCode)
HttpStatus::isError(int $statusCode)
HttpStatus::getReasonPhrase(int $statusCode)

The Versions

23/06 2018

dev-master

9999999-dev

An easy to use wrapper for the curl http library in PHP

  Sources   Download

MIT

The Requires

  • php >=7.0
  • ext-curl *
  • ext-dom *

 

The Development Requires

by Marius Waldhorst

http client http-client

23/06 2018

1.0.0-alpha

1.0.0.0-alpha

An easy to use wrapper for the curl http library in PHP

  Sources   Download

MIT

The Requires

  • php >=7.0
  • ext-curl *
  • ext-dom *

 

The Development Requires

by Marius Waldhorst

http client http-client

18/06 2018

0.1.3

0.1.3.0

An easy to use wrapper for the curl http library in PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

by Marius Waldhorst

http client http-client

11/11 2017

0.1.2

0.1.2.0

An easy to use wrapper for the curl http library in PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

by Marius Waldhorst

http client http-client

20/07 2017

0.1.1

0.1.1.0

An easy to use wrapper for the curl http library in PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

by Marius Waldhorst

http client http-client

16/07 2017

0.1.0

0.1.0.0

An easy to use wrapper for the curl http library in PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

by Marius Waldhorst

http client http-client

02/05 2017

0.1.0-alpha

0.1.0.0-alpha

An easy to use wrapper for the curl http library in PHP

  Sources   Download

MIT

The Requires

  • php >=7.0

 

The Development Requires

by Marius Waldhorst

http client http-client