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

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

p3k/http
========

A simple wrapper API around the PHP curl functions

0.1.13(1y ago)1426.7k↑89.5%4[2 PRs](https://github.com/aaronpk/p3k-http/pulls)MITPHP

Since Apr 28Pushed 1y ago2 watchersCompare

[ Source](https://github.com/aaronpk/p3k-http)[ Packagist](https://packagist.org/packages/p3k/http)[ Docs](https://github.com/aaronpk/p3k-http)[ RSS](/packages/p3k-http/feed)WikiDiscussions main Synced today

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

p3k-http
========

[](#p3k-http)

A simple HTTP client, used by  projects.

Usage
-----

[](#usage)

### GET

[](#get)

```
$http = new p3k\HTTP();
$headers = [
  'Accept: text/html, */*'
];
$response = $http->get('http://example.com/', $headers);
```

### POST

[](#post)

```
$http = new p3k\HTTP();
$headers = [
  'Accept: application/json',
  'Content-type: application/json'
];
$response = $http->post('http://example.com/', json_encode([
  'foo' => 'bar'
], $headers);
```

### HEAD

[](#head)

```
$http = new p3k\HTTP();
$headers = [
  'Accept: text/html, */*'
];
$response = $http->head('http://example.com/', $headers);
```

### Response

[](#response)

The get/post/head functions will return an array with the following properties:

- `code` - integer, the HTTP response code that was returned
- `headers` - array, the HTTP headers returned
- `rels` - array, the parsed HTTP rel values from any `Link` headers
- `body` - string, the body of the HTTP response, or false/omit for a HEAD request
- `error` - string, an error string. see below for the enumerated list.
- `error_description` - string,
- `url` - string, the final URL retrieved after following any redirects
- `debug` - string, the full HTTP response

#### `headers`

[](#headers)

The `headers` key will be an array of all the header values returned. The values will be either a string or array depending on whether there were multiple values returned for a given header name.

```
    [headers] => Array
        (
            [Server] => nginx/1.12.0
            [Content-Type] => text/html; charset=UTF-8
            [Transfer-Encoding] => chunked
            [Connection] => keep-alive
            [Cache-Control] => no-cache
            [Link] => Array
                (
                    [0] => ; rel="hub"
                    [1] => ; rel="authorization_endpoint"
                    [2] => ; rel="micropub"
                    [3] => ; rel="token_endpoint"
                    [4] => ; rel="self"
                )

            [Date] => Fri, 28 Apr 2017 18:40:42 GMT
            [Strict-Transport-Security] => max-age=2592000
            [X-No-Cache] => 0
            [X-Cache] => EXPIRED
        )

```

#### `rels`

[](#rels)

The `rels` key will be the parsed version of any HTTP `Link` headers that contain a rel value. All values will be arrays even if there is only one value.

```
    [rels] => Array
        (
            [hub] => Array
                (
                    [0] => https://switchboard.p3k.io/
                )

            [authorization_endpoint] => Array
                (
                    [0] => https://aaronparecki.com/auth
                )

            [micropub] => Array
                (
                    [0] => https://aaronparecki.com/micropub
                )

            [token_endpoint] => Array
                (
                    [0] => https://aaronparecki.com/auth/token
                )

            [self] => Array
                (
                    [0] => https://aaronparecki.com/
                )
        )

```

### Options

[](#options)

There are a few options you can set when making HTTP requests, to configure how the request will behave.

- `$http->set_user_agent('String')` - A shortcut for setting the user agent header.
- `$http->set_max_redirects(2)` - The maximum number of redirects to follow. Defaults to 8.
- `$http->set_timeout(10)` - The timeout in seconds for waiting for a response. Defaults to 4.

#### User Agent

[](#user-agent)

You can set the user agent when you first instantiate the HTTP object:

```
$http = new p3k\HTTP('Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36 p3k-http/0.1.0');
$http->get('http://example.com/');
```

Alternately, you can change it before each request:

```
$http = new p3k\HTTP();
$http->set_user_agent('Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36 p3k-http/0.1.0');
$http->get('http://example.com/');
```

Transports
----------

[](#transports)

By default, the library will use the PHP curl functions to make the HTTP request.

You can optionally define your own transport to use to make HTTP requests instead. This allows you to use an existing HTTP request mechanism you may have rather than using curl or the PHP stream functions.

Define a new class that implements the `p3k\HTTP\Transport` interface. That interface documents the return values expected as well.

Then you can set the transport after you create the main HTTP object.

```
$http = new p3k\HTTP();
$http->set_transport(new p3k\HTTP\Stream());  // or your custom class here
```

The library ships with two alternative transport mechanisms, `p3k\HTTP\Stream` and `p3k\HTTP\Test`. The Stream transport uses `file_get_contents` with all the necessary config options to make it work. This is useful when running in Google App Engine. The Test transport will read HTTP responses from files, so that you can write tests that simulate making HTTP calls. See  for an example of using this transport.

License
-------

[](#license)

Copyright 2017 by Aaron Parecki

Available under the MIT license.

###  Health Score

41

—

FairBetter than 89% of packages

Maintenance39

Infrequent updates — may be unmaintained

Popularity36

Limited adoption so far

Community12

Small or concentrated contributor base

Maturity61

Established project with proven stability

 Bus Factor1

Top contributor holds 94.1% 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 ~210 days

Recently: every ~426 days

Total

14

Last Release

520d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/385b302aef27e9e72ddfbf65c8b15cfce5946744fe2e541c5b415665fc2351fc?d=identicon)[aaronpk](/maintainers/aaronpk)

---

Top Contributors

[![aaronpk](https://avatars.githubusercontent.com/u/113001?v=4)](https://github.com/aaronpk "aaronpk (16 commits)")[![Zegnat](https://avatars.githubusercontent.com/u/490579?v=4)](https://github.com/Zegnat "Zegnat (1 commits)")

---

Tags

httpp3k

### Embed Badge

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

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

###  Alternatives

[symfony/routing

Maps an HTTP request to a set of configuration variables

7.6k775.6M1.6k](/packages/symfony-routing)[friendsofsymfony/rest-bundle

This Bundle provides various tools to rapidly develop RESTful API's with Symfony

2.8k72.4M299](/packages/friendsofsymfony-rest-bundle)[php-http/discovery

Finds and installs PSR-7, PSR-17, PSR-18 and HTTPlug implementations

1.3k302.1M1.1k](/packages/php-http-discovery)[nelmio/api-doc-bundle

Generates documentation for your REST API from attributes

2.3k62.5M221](/packages/nelmio-api-doc-bundle)[api-platform/core

Build a fully-featured hypermedia or GraphQL API in minutes!

2.6k47.0M210](/packages/api-platform-core)[spatie/crawler

Crawl all internal links found on a website

2.8k15.7M46](/packages/spatie-crawler)

PHPackages © 2026

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