PHPackages                             chh/httpfetch - 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. chh/httpfetch

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

chh/httpfetch
=============

A library for simple HTTP requests (using Guzzle Ring)

v1.0.1(11y ago)515.0k2[1 issues](https://github.com/CHH/httpfetch/issues)MITPHPPHP &gt;=5.4.0

Since May 10Pushed 11y ago1 watchersCompare

[ Source](https://github.com/CHH/httpfetch)[ Packagist](https://packagist.org/packages/chh/httpfetch)[ Docs](https://github.com/CHH/httpfetch.git)[ RSS](/packages/chh-httpfetch/feed)WikiDiscussions master Synced 1w ago

READMEChangelog (2)Dependencies (2)Versions (3)Used By (0)

httpfetch
=========

[](#httpfetch)

[![Build Status](https://camo.githubusercontent.com/72d3f772a04c4580cc17d253a4f0b6be2642b3d6fe0e6680aa0d8b589970bd9b/68747470733a2f2f7472617669732d63692e6f72672f4348482f6874747066657463682e737667)](https://travis-ci.org/CHH/httpfetch)[![Latest Stable Version](https://camo.githubusercontent.com/d227c4e7e602ee74617611a9575f3589f969b6230224af0f36a802ac381075e9/68747470733a2f2f706f7365722e707567782e6f72672f6368682f6874747066657463682f762f737461626c65)](https://packagist.org/packages/chh/httpfetch)

httpfetch provides a simple function `fetch` to make HTTP requests in small scripts easy and fast.

httpfetch relies heavily on [RingPHP](https://github.com/guzzle/RingPHP) for its robust low level HTTP abstraction and for the ability to make asynchronous requests.

Use Cases
---------

[](#use-cases)

- As replacement for `file_get_contents`, but with async support, proper support for all HTTP features, easy passing of request headers, parsing of response headers, and proper handling of the certificates used by HTTPS.
- `file_get_contents` is notoriously insecure when used with HTTPS without proper configuration, which is hard to do. Also see this [secure file\_get\_contents wrapper](https://github.com/padraic/file_get_contents).
- Making simple web service clients without depending on Guzzle.

Install
-------

[](#install)

Via Composer

```
$ composer require chh/httpfetch
```

Usage
-----

[](#usage)

```
use function chh\httpfetch\fetch;

$response = fetch("https://www.example.com");

echo stream_get_contents($response['body']);
```

The chh\\httpfetch Namespace
----------------------------

[](#the-chhhttpfetch-namespace)

### fetch($url, array $options = \[\])

[](#fetchurl-array-options--)

Makes a HTTP request to the provided URL. The options follow the [RingPHP specification for requests](http://ringphp.readthedocs.org/en/latest/spec.html#requests) and the returned response follows the [RingPHP specification for responses](http://ringphp.readthedocs.org/en/latest/spec.html#responses).

All requests are made asynchronously by default, when supported by the handler. This can be turned off by setting the `future` option to `false`.

httpfetch implements a few additional options for convenience:

- `follow_location` (default: `true`): Follows responses which return a "Location" header
- `max_redirects` (default: `10`): Number of redirects to follow
- `auth` (default: `null`): Username/password pair as array for Basic Authentication, e.g. `["user", "password"]`

Responses are array-like objects with the following keys:

- `body`: (string, fopen resource, Iterator, GuzzleHttp\\Stream\\StreamInterface) The body of the response, if present. Can be a string, resource returned from fopen, an Iterator that yields chunks of data, an object that implemented \_\_toString, or a GuzzleHttp\\Stream\\StreamInterface.
- `effective_url`: (string) The URL that returned the resulting response.
- `error`: (\\Exception) Contains an exception describing any errors that were encountered during the transfer.
- `headers`: (Required, array) Associative array of headers. Each key represents the header name. Each value contains an array of strings where each entry of the array is a header line. The headers array MAY be an empty array in the event an error occurred before a response was received.
- `reason`(string) Optional reason phrase. This option should be provided when the reason phrase does not match the typical reason phrase associated with the status code. See RFC 7231 for a list of HTTP reason phrases mapped to status codes.
- `status`: (Required, integer) The HTTP status code. The status code MAY be set to null in the event an error occurred before a response was received (e.g., a networking error).
- `transfer_stats`: (array) Provides an associative array of arbitrary transfer statistics if provided by the underlying handler.
- `version`: (string) HTTP protocol version. Defaults to 1.1.

For example a POST request by using the `http_method` parameter:

```
$response = fetch('http://www.example.com', [
    'http_method' => 'POST',
    'body' => 'foo'
]);

var_dump($response['status']);
var_dump(stream_get_contents($response['body']));
```

Example: Doing an async GET request with the Promise API:

```
fetch('http://www.example.com')->then(function ($response) {
  // Save the response stream:
  $out = fopen('/tmp/foo.txt', 'w+b');
  stream_copy_to_stream($response['body'], $out);
  fclose($out);
});
```

Example: Doing an async request with the Future API:

```
$response = fetch('http://www.example.com');

// Do some other stuff

$response->wait();

echo stream_get_contents($response['body']);
```

Example: Doing requests in parallel:

```
$response1 = fetch('http://www.example.com');
$response2 = fetch('http://www.foo.com');

echo $response1['status'], "\n";
echo $response2['status'], "\n";
```

### get(), post(), put(), delete(), head(), options()

[](#get-post-put-delete-head-options)

Helper methods for common HTTP methods. They all follow the same signature of `($url, array $options = [])`.

Example:

```
use chh\httpfetch;

// GET request
$response = httpfetch\get("https://www.example.com");

// POST request with body
$response = httpfetch\post("https://www.example.com", [
    'body' => 'foo',
]);
```

### set\_default\_handler(callable $handler)

[](#set_default_handlercallable-handler)

Overrides the Guzzle Ring Client handler which is used by the `fetch` function. Handlers are callables which follow the [Ring specification](http://ringphp.readthedocs.org/en/latest/spec.html). Reset the handler to the default by passing `null`.

Example: Force the usage of PHP's http:// stream wrapper:

```
chh\httpfetch\set_default_handler(new Guzzle\Ring\Client\StreamHandler);

fetch('http://example.com');
```

Testing
-------

[](#testing)

```
$ make test
```

Contributing
------------

[](#contributing)

Please see [CONTRIBUTING](CONTRIBUTING.md) for details.

Security
--------

[](#security)

If you discover any security related issues, please email  instead of using the issue tracker.

Credits
-------

[](#credits)

- [Christoph Hochstrasser](https://github.com/CHH)
- [All Contributors](../../contributors)

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

31

—

LowBetter than 66% of packages

Maintenance16

Infrequent updates — may be unmaintained

Popularity26

Limited adoption so far

Community9

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

Total

2

Last Release

4042d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/f1d7b35fc21b94c4c392b0198b3f3a9060e56b0c962fab4226ad5699cade7052?d=identicon)[chh](/maintainers/chh)

---

Top Contributors

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

---

Tags

httphttpfetchringphp

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/chh-httpfetch/health.svg)

```
[![Health](https://phpackages.com/badges/chh-httpfetch/health.svg)](https://phpackages.com/packages/chh-httpfetch)
```

###  Alternatives

[guzzlehttp/psr7

PSR-7 message implementation that also provides common utility methods

7.9k1.1B3.7k](/packages/guzzlehttp-psr7)[psr/http-message

Common interface for HTTP messages

7.0k1.1B6.5k](/packages/psr-http-message)[psr/http-factory

PSR-17: Common interfaces for PSR-7 HTTP message factories

1.9k728.6M2.4k](/packages/psr-http-factory)[php-http/httplug

HTTPlug, the HTTP client abstraction for PHP

2.6k319.6M738](/packages/php-http-httplug)[psr/http-client

Common interface for HTTP clients

1.7k714.1M2.7k](/packages/psr-http-client)[symfony/http-client

Provides powerful methods to fetch HTTP resources synchronously or asynchronously

2.1k330.1M4.5k](/packages/symfony-http-client)

PHPackages © 2026

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