PHPackages                             rehyved/php-http-client - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [HTTP &amp; Networking](/categories/http)
4. /
5. rehyved/php-http-client

ActiveLibrary[HTTP &amp; Networking](/categories/http)

rehyved/php-http-client
=======================

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

0.1.3(7y ago)11.0k1MITPHPPHP &gt;=7.0

Since Jul 16Pushed 7y ago2 watchersCompare

[ Source](https://github.com/Rehyved/php-http-client)[ Packagist](https://packagist.org/packages/rehyved/php-http-client)[ RSS](/packages/rehyved-php-http-client/feed)WikiDiscussions master Synced 2mo ago

READMEChangelog (5)Dependencies (2)Versions (6)Used By (1)

HTTP Client for PHP
===================

[](#http-client-for-php)

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

[![Build Status](https://camo.githubusercontent.com/ec79df325a086f8851e3a8bf2ca09c371f67d58cc1edf442035779b2ba5c7681/68747470733a2f2f7472617669732d63692e6f72672f526568797665642f7068702d687474702d636c69656e742e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/Rehyved/php-http-client)

This library uses a builder pattern similar to the [Apache HTTP client Fluent API](https://hc.apache.org/httpcomponents-client-ga/tutorial/html/fluent.html) for Java. The goal is to provide a readable and maintainable way of writing HTTP request logic inside your PHP websites and applications.

Feedback is always welcome.

- [Installation](#installation)
- [Usage](#usage)
    - [HttpRequest](#httprequest-class)
        - [Overriding default configuration](#overriding-default-configuration)
        - [Request types](#request-types)
            - [GET](#get-request)
            - [PUT](#put-request)
            - [POST](#post-request)
            - [DELETE](#delete-request)
        - [Adding query parameters](#adding-query-parameters)
        - [Adding headers](#adding-headers)
        - [Adding cookies](#adding-cookies)
        - [Basic authentication](#basic-authentication)
        - [Authorization header](#authorization-header)
        - [Changing request timeout](#changing-request-timeout)
        - [Disabling SSL certificate verification](#disabling-ssl-certificate-verification)
    - [HttpResponse](#httpresponse-class)
    - [HttpStatus](#httpstatus-class)

Installation
------------

[](#installation)

**Prerequisites**:

- PHP 7.0 or higher
- PHP cURL module (ext-curl)
- PHP DOM module (ext-dom)

This library is available through [Packagist](https://packagist.org) and can be imported using [Composer](https://getcomposer.org):

```
composer require rehyved/php-http-client

```

Usage
-----

[](#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](#httprequest-class) and the [HttpResponse class](#httpresponse-class).

### HttpRequest class

[](#httprequest-class)

The following examples show different usages of the HttpRequest class to perform HTTP requests:

#### Overriding default configuration

[](#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.

The following configuration options are available by defining the appropriate constants with [define()](http://php.net/manual/en/function.define.php):

- **RPHC\_DEFAULT\_HEADERS** - An associative array of header name-&gt; 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

[](#request-types)

##### GET request

[](#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*

##### PUT request

[](#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

[](#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

[](#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

[](#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

[](#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

[](#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

[](#basic-authentication)

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

#### Authorization header

[](#authorization-header)

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

#### Changing request timeout

[](#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

[](#disabling-ssl-certificate-verification)

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

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

### HttpResponse class

[](#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.

#### Status handling

[](#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

[](#header-handling)

```
$contentType = $response->getContentType();
$header = $response->getHeader("Content-Type");
$headers = $response->getHeaders(); // an associative array of header name -> header value
```

#### Cookie handling

[](#cookie-handling)

```
$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

[](#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

[](#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.

#### Constants

[](#constants)

The class provides constants for the HTTP statuses, for example:

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

etc...
```

#### Methods

[](#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)
```

###  Health Score

27

—

LowBetter than 49% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity15

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity52

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~85 days

Total

5

Last Release

2877d ago

Major Versions

0.1.3 → 1.0.0-alpha2018-06-23

### Community

Maintainers

![](https://www.gravatar.com/avatar/4a0bae73c69899a9e824dad4c715cea83b21727ca926b8f12caea90921539bd2?d=identicon)[mpwaldhorst](/maintainers/mpwaldhorst)

---

Top Contributors

[![mpwaldhorst](https://avatars.githubusercontent.com/u/13185130?v=4)](https://github.com/mpwaldhorst "mpwaldhorst (29 commits)")

---

Tags

clienthttphttp-requestsphpphp-curlphp-libraryphp7httpclienthttp client

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/rehyved-php-http-client/health.svg)

```
[![Health](https://phpackages.com/badges/rehyved-php-http-client/health.svg)](https://phpackages.com/packages/rehyved-php-http-client)
```

###  Alternatives

[aplus/http-client

Aplus Framework HTTP Client Library

2161.6M1](/packages/aplus-http-client)[m6web/guzzle-http-bundle

Symfony bundle on top of Guzzle

17511.5k2](/packages/m6web-guzzle-http-bundle)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
