PHPackages                             originphp/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. originphp/http-client

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

originphp/http-client
=====================

OriginPHP HTTP Client

3.1.1(5y ago)27382MITPHPPHP &gt;=7.3.0CI failing

Since Oct 12Pushed 5y ago1 watchersCompare

[ Source](https://github.com/originphp/http-client)[ Packagist](https://packagist.org/packages/originphp/http-client)[ Docs](https://www.originphp.com)[ RSS](/packages/originphp-http-client/feed)WikiDiscussions master Synced 4d ago

READMEChangelogDependencies (3)Versions (11)Used By (2)

HTTP Client
===========

[](#http-client)

[![license](https://camo.githubusercontent.com/6fdb99389fe9d9e8a5c197002a191ace7c8b12a2020c0fa5756cf17aa08a4966/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874477265656e2e737667)](https://camo.githubusercontent.com/6fdb99389fe9d9e8a5c197002a191ace7c8b12a2020c0fa5756cf17aa08a4966/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874477265656e2e737667)[![build](https://github.com/originphp/http-client/workflows/CI/badge.svg)](https://github.com/originphp/http-client/actions)[![coverage](https://camo.githubusercontent.com/17b12c925780ccf3dd161c6b1e2186cd1fec603626a02b70ae6255b6ac91e547/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6769746875622f6f726967696e7068702f687474702d636c69656e742f62616467652e7376673f6272616e63683d6d6173746572)](https://coveralls.io/github/originphp/http-client?branch=master)

The HTTP client is a simple yet very powerful utility for making HTTP requests.

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

[](#installation)

To install this package

```
$ composer require originphp/http-client

```

Sending Requests
----------------

[](#sending-requests)

### Get Request

[](#get-request)

To send a GET request

```
use Origin\HttpClient\Http;
$http = new Http();
$response = $http->get('https://api.example.com/posts');

// To use query parameters. https://api.example.com/posts?api_token=1234-1234-1234-1234
$response = $http->get('https://api.example.com/posts',[
    'query' => ['api_token' => '1234-1234-1234-1234']
]);

// An example with more options
$response = $http->get('https://api.example.com/posts',[
    'headers' => ['Accept' => 'application/json'],
    'cookies' => ['name' => 'value'],
    'userAgent' => 'MyApp',
    'referer' => 'https://www.somewebsite.com/search'
]);
```

The full list of options are detailed below.

### Head Request

[](#head-request)

To make HEAD request, where the body of the request is not fetched.

```
use Origin\HttpClient\Http;
$http = new Http();
$response = $http->head('https://api.example.com/posts');

// To use query parameters. https://api.example.com/posts?api_token=1234-1234-1234-1234
$response = $http->head('https://api.example.com/posts',[
    'query' => ['api_token' => '1234-1234-1234-1234']
]);

// An example with more options
$response = $http->head('https://api.example.com/posts',[
    'headers' => ['Accept' => 'application/json'],
    'cookies' => ['name' => 'value'],
    'userAgent' => 'MyApp',
    'referer' => 'https://www.somewebsite.com/search'
]);
```

### Post Request

[](#post-request)

To send a POST request. In REST terms post requests are used to create a record.

```
use Origin\HttpClient\Http;
$http = new Http();

// to send a post request with empty data
$response = $http->post('https://api.example.com/posts');

// to send a post request with data
$response = $http->post('https://api.example.com/posts',[
    'data' => [
        'title' => 'Article Title',
        'body' => 'Article body'
    ]
]);

// example with other options
$response = $http->post('https://api.example.com/posts',[
    'data' => [
        'title' => 'Article Title',
        'body' => 'Article body'
    ],
    'headers' => ['Accept' => 'application/json'],
    'cookies' => ['name' => 'value'],
    'userAgent' => 'MyApp',
    'referer' => 'https://www.somewebsite.com/search'
]);
```

To upload files using a post request.

```
use Origin\HttpClient\Http;
$http = new Http();
$response = $http->post('https://api.example.com/posts',[
    'data' => [
        'contacts' => Http::file('/docs/contacts.csv')
    ]
]);

// Shortcut for Http::file
$response = $http->post('https://api.example.com/posts',[
    'data' => [
        'contacts' => '@/docs/contacts.csv'
    ]
]);
```

### Put Request

[](#put-request)

To send a PUT request. In REST terms put requests are used to modify a record with complete data (overwriting).

```
use Origin\HttpClient\Http;
$http = new Http();
$response = $http->put('https://api.example.com/posts/1',[
    'data' => [
        'title' => 'Changed Article Title',
        'body' => 'Article body'
    ],
]);
```

### Patch Request

[](#patch-request)

To send a PATCH request. In REST terms patch requests are used to modify an existing record with partial data.

```
use Origin\HttpClient\Http;
$http = new Http();
$response = $http->patch('https://api.example.com/posts/1',[
     'data' => [
        'title' => 'Another Article Title',
    ],
]);
```

### Delete Request

[](#delete-request)

To send a DELETE request.

```
use Origin\HttpClient\Http;
$http = new Http();
$response = $http->delete('https://api.example.com/posts/1');

// Example passing some options
$response = $http->delete('https://api.example.com/posts/1',[
    'userAgent' => 'OriginPHP'
]);
```

HTTP Request Options
--------------------

[](#http-request-options)

The available options when making requests are

- query: appends vars to the query. e.g \['api\_token' =&gt; '1234-1234-1234-1234'\]
- userAgent: the name of the user agent to use e.g. 'originPHP'
- referer: default null. The url of the referer e.g. ''
- redirect: default true. set to false to not follow redirects
- timeout: default timeout is 30 seconds
- cookieJar: default true. Persists cookies for instance. Set to false to not perist cookies. Pass a string with filename and path to save to and read cookies from. e.g. '/var/www/data/cookies.data'
- type: request and accept content type (json xml) e.g. 'json'
- auth: authtentication details. An array with username, password, and type (basic|digest|nltm)
- proxy: proxy server details. An array with proxy, username, password.
- curl: an array of curl options either string or constant e.g \[CURLOPT\_SSL\_VERIFYHOST=&gt;0, 'ssl\_verifypeer'=&gt;0\]
- headers: an array of headers to set. e.g \['Accept' =&gt; 'application/json'\]
- cookies: an array of cookies to set. e.g. \['name' =&gt; 'value'\]
- fields: This option is for post/put/patch requests. Its an array of fields to be posted e.g. \['title' =&gt; 'Article #1','description' =&gt; 'An article'\]

Configuring the Http Client
---------------------------

[](#configuring-the-http-client)

You configure Http client so that you don't have to keep on passing options which makes code longer and more prone to errors.

This is particularly useful when working with multiple requests in an instance. Any options passed when creating the instance will be used as default for each request, unless you specify something else during the request.

```
use Origin\HttpClient\Http;
$http = new Http([
    'base' => 'https://www.example.com/api'
]);
$response = $http->get('/posts');
```

Other options:

- userAgent
- timeout
- cookieJar
- type
- auth
- proxy
- referer
- headers
- cookies
- verbose

Exceptions (version ^2.0)
-------------------------

[](#exceptions-version-20)

In 2.0 various exceptions have been added, and a HTTP protocol error handler has also been added. All exceptions from the http client extend the `HttpClientException` class.

### Request Exceptions

[](#request-exceptions)

In the event of connection issues (DNS, timeout etc), a `ConnectionException` will be thrown, and a `TooManyRedirectsException` will be thrown on redirect loops, and any other cURL error will trigger a generic `RequestException`.

### HTTP Protocol Exceptions

[](#http-protocol-exceptions)

By default any 4xx and 5xx errors will throw either `ClientErrorException` or `ServerErrorException` which both extend the `HttpException` class.

This behavior can be disabled by setting `httpErrors` to `false` when creating the `Http` instance.

```
$http = new Http([
    'httpErrors' => false
]);
```

To catch HTTP protocol errors

```
try {
    (new Http())->get('http://wwww.google.com');
} catch (HttpException $exception) {
    // do something
}
```

Working with Responses
----------------------

[](#working-with-responses)

When you make a HTTP request, a `Response` object is returned.

```
use Origin\HttpClient\Http;
$http = new Http();
$response = $http->head('https://api.example.com/posts');

// working with the response body
$data = $response->body();
$data = $response->json(); // decodes a JSON response into an array
$data = $response->xml(); // converts XML response into an array

// Response stuff
$headers = $response->headers(); // headers
$cookies = $response->cookies(); // cookies from the *response*
$code = $response->statusCode();

// Assertions
$bool = $response->ok(); // has status code of 200
$bool = $response->success(); // has any 20x status code
$bool = $response->redirect(); // has a redirect status code 30x
```

Cookies
-------

[](#cookies)

By default cookies are persisted across all requests for the instance.

To change this behavior use the `cookieJar` option.

```
use Origin\HttpClient\Http;

// Persist cookies for this session only (stores in array)
$http = new Http([
    'cookieJar' => true
]);

// Don't use cookies
$http = new Http([
    'cookieJar' => false;
]);

// Persist cookies to file
$http = new Http([
    'cookieJar' => '/var/www/data/cookies.data';
]);
```

### cURL Options

[](#curl-options)

Occasionally, you might need to set additional cURL options, one example of this, is when there is an issue with SSL certificates. You can set cURL options with the CURLOPT constant or string version of it.

```
use Origin\HttpClient\Http;
$http = new Http();
$response = $http->get('https://api.example.com/posts',[
    'curl' => [
        CURLOPT_SSL_VERIFYHOST => 0,
        CURLOPT_SSL_VERIFYPEER => 0
        ]
]);

$http = new Http();
$response = $http->get('https://api.example.com/posts',[
    'curl' => [
        'ssl_verifyhost' => 0,
        'ssl_verifypeer' => 0
        ]
]);
```

To set these as default for settings for all the requests, configure it when creating the Http instance.

```
use Origin\HttpClient\Http;
$http = new Http([
    'curl' => [
        'ssl_verifyhost' => 0,
        'ssl_verifypeer' => 0
        ]
    ]);
```

### Downloading a file

[](#downloading-a-file)

To download a file

```
$path =  '/var/www/file.tar.gz';
$fp = fopen($path, 'w');

$http = new Http();
$response = $http->get('https://example.com/file.tar.gz',[
    'curl' => [
        CURLOPT_HEADER => false,
        CURLOPT_FILE => $fp
    ]
]);

fclose($fp);
```

### Setting Cookies

[](#setting-cookies)

To set a cookie

```
use Origin\HttpClient\Http;
$response = $http->get('https://api.example.com/posts',[
    'cookies' => [
        'name' => 'value'
        ]
    ]);
```

### User Authentication

[](#user-authentication)

The available authentication types are `basic`, `digest`, `nltm` or `any`.

```
use Origin\HttpClient\Http;
$response = $http->get('https://api.example.com/posts',[
    'auth' => [
        'username' => 'foo',
        'password' => 'secret',
        'type' => 'digest
        ]
    ]);
```

### Using a Proxy

[](#using-a-proxy)

To send a HTTP request using a proxy server.

```
use Origin\HttpClient\Http;

// without authentication
$response = $http->get('https://api.example.com/posts',[
    'proxy' => [
        'proxy' => 'https://www.proxy.com:8080'
        ]
    ]);

// with authentication
$response = $http->get('https://api.example.com/posts',[
    'proxy' => [
        'proxy' => 'https://www.proxy.com:8080',
        'username' => 'foo',
        'password' => 'secret'
        ]
    ]);
```

###  Health Score

29

—

LowBetter than 59% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity16

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity59

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 ~52 days

Recently: every ~45 days

Total

10

Last Release

1930d ago

Major Versions

1.0.3 → 2.0.02020-06-05

2.0.2 → 3.0.02020-11-29

PHP version history (3 changes)1.0.0PHP ^7.2.0

2.0.2PHP &gt;=7.2.0

3.0.0PHP &gt;=7.3.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/7e8a821333d9c7b7bc2ad3d164d142f65cd3912dea78033d31f76b0f19ba8a0c?d=identicon)[originphp](/maintainers/originphp)

---

Top Contributors

[![jamielsharief](https://avatars.githubusercontent.com/u/20553479?v=4)](https://github.com/jamielsharief "jamielsharief (36 commits)")

---

Tags

httpclientrestcurlhttp clientoriginPHP

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Type Coverage Yes

### Embed Badge

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

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

###  Alternatives

[zoonman/pixabay-php-api

PixabayClient is a PHP HTTP client library to access Pixabay's API

3354.7k](/packages/zoonman-pixabay-php-api)[e-moe/guzzle6-bundle

Integrates Guzzle 6 into your Symfony application

11259.2k](/packages/e-moe-guzzle6-bundle)[opgg/riotquest

RiotQuest, PHP RiotAPI client library that focused on multi request from OP.GG

172.6k](/packages/opgg-riotquest)

PHPackages © 2026

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