PHPackages                             stubbles/peer - 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. stubbles/peer

ActiveLibrary

stubbles/peer
=============

Help with socket operations.

v11.0.0(5mo ago)02.5k[1 PRs](https://github.com/stubbles/stubbles-peer/pulls)2BSD-3-ClausePHPPHP ^8.3CI passing

Since Jan 11Pushed 1mo ago2 watchersCompare

[ Source](https://github.com/stubbles/stubbles-peer)[ Packagist](https://packagist.org/packages/stubbles/peer)[ Docs](http://stubbles.net/)[ RSS](/packages/stubbles-peer/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (10)Dependencies (5)Versions (11)Used By (2)

stubbles/peer
=============

[](#stubblespeer)

Help with socket operations.

Build status
------------

[](#build-status)

[![Tests](https://github.com/stubbles/stubbles-peer/workflows/Tests/badge.svg)](https://github.com/stubbles/stubbles-peer/workflows/Tests/badge.svg)

[![Latest Stable Version](https://camo.githubusercontent.com/1ba28ca316935d5d2a2f73d6aba2ebbbaa4b5e03e6744f16f9e581bbeb706981/68747470733a2f2f706f7365722e707567782e6f72672f73747562626c65732f706565722f76657273696f6e2e706e67)](https://packagist.org/packages/stubbles/peer) [![Latest Unstable Version](https://camo.githubusercontent.com/0306501956ea51c3aae249e350e5543a788ee04fb0c81af72581b3d6e2f68367/68747470733a2f2f706f7365722e707567782e6f72672f73747562626c65732f706565722f762f756e737461626c652e706e67)](//packagist.org/packages/stubbles/peer)

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

[](#installation)

*stubbles/peer* is distributed as [Composer](https://getcomposer.org/)package. To install it as a dependency of your package use the following command:

```
composer require "stubbles/peer": "^11.0"

```

Requirements
------------

[](#requirements)

*stubbles/peer* requires at least PHP 8.3.

Working with URIs
-----------------

[](#working-with-uris)

Sometimes it's useful to have a URI wrapped into a class which provides methods to work with this URI. Stubbles Core provides `stubbles\peer\Uri` for such cases.

New instances can be created via `Uri::fromString('ftp://user@example.net/');`. The following rules apply:

- If the supplied uri string is empty no instance will be returned, but `null`instead.
- If the supplied uri string is not a valid URI a `stubbles\peer\MalformedUri`will be thrown.
- For all other cases, an instance of `stubbles\peer\Uri` is returned.
- Since release 8.0.0 using passwords in URIs is discouraged, and support for passwords in URIs will be removed with 9.0.0. Generally, protocols provide other and better means to transport the password, as using it in URIs is inherently insecure.

In order for a uri string to be a valid URI it must adhere to the specification laid out in [RFC 3986](https://www.ietf.org/rfc/rfc3986.txt).

Please note that hostnames will be normalized, which means if the given hostname is e.g. *eXAMple.net*, it will be normalized to *example.net* and always returned in normalized form.

For the methods, the following rules apply:

- `hasDnsRecord()` returns `false` if the URI does not contain a host.
- `hasDnsRecord()`always returns true for *localhost*, *127.0.0.1* and *\[::1\]*.
- `hasDefaultPort()` returns `false` when a port is specified, even if it might be the default port for the scheme. This method is meant for child classes which provide additional methods for certain protocols.

URI instances can only be changed regarding their URI parameters. It is not possible to change the scheme, host, user, password, port, or fragment of the URI.

Working with HTTP URIs
----------------------

[](#working-with-http-uris)

While the basic implementation for URIs already provides useful help when working with URIs, sometimes one needs slightly better support for HTTP URIs. *stubbles/peer* provides `stubbles\peer\http\HttpUri` for such cases.

New instances can be created via `HttpUri::fromString('http://example.net/');`. The following rules apply:

- If the supplied uri string is empty no instance will be returned, but `null`instead.
- If the supplied uri string is not a valid HTTP URI a `stubbles\peer\MalformedUri`will be thrown.
- For all other cases, an instance of `stubbles\peer\http\HttpUri` is returned.

In order for a uri string to be a valid URI it must adhere to the specification laid out in [RFC 7230](https://www.ietf.org/rfc/rfc7230.txt). Any uri strings with other schemes than *http* or *https* are rejected and lead to a thrown `stubbles\peer\MalformedUri`.

Additionally, instances can be created using `HttpUri::fromParts($scheme, $host, $port = null, $path = '/', $queryString = null)`. *(Available since release 4.0.0.)*

### Rules for specific methods

[](#rules-for-specific-methods)

- `hasDefaultPort()` returns `true` if the scheme is *http* and the port is 80. In case no port was originally supplied, port 80 is assumed. The method also returns `true` if the scheme is *https* and the port is 443. In case no port was originally supplied, port 443 is assumed. In any other case the method returns `false`.
- `port()` will return the port if it was originally supplied. If it was not supplied and scheme is *http* return value will be 80, and if scheme is *https* return value will be 443.

### Changing portions of the HTTP URI

[](#changing-portions-of-the-http-uri)

Instances of `HttpUri` can only be changed regarding their URI parameters. It is not possible to change the host, user, password, port, or fragment of the URI. Additionally it is possible to change the scheme, but this will return a new instance:

- `toHttp()`: If the scheme is *http* the same instance will be returned. If the scheme is *https* a new instance with the same URI but scheme *http*will be returned.
- `toHttps()`: If the scheme is *https* the same instance will be returned. If the scheme is *http* a new instance with the same URI but scheme *https*will be returned.

The current scheme can be checked with `isHttp()` and `isHttps()`.

### Establish connections to HTTP URIs

[](#establish-connections-to-http-uris)

Additionally, the class provides possibilities to establish connections to the name HTTP URI:

- `openSocket()` will create a `stubbles\peer\Socket` instance to which one can connect. See section on sockets for more details.
- `connect()` provides higher level access with a full HTTP connection. The method can optionally take an instance of `stubbles\peer\HeaderList` from which headers will be applied to the request.

### Establishing a HTTP connection

[](#establishing-a-http-connection)

A HTTP request to the target URI can be done in the following way:

```
$response = $httpUri->connect()
        ->asUserAgent('Not Mozilla')
        ->timeout(5)
        ->usingHeader('X-Money', 'Euro')
        ->get();
```

Please note that the call to `connect()` does not open the connection, but establishes it locally only. Rather, it can be used to add some more headers to the request: user agent, referer, cookies or any other header. Only the last method really opens the connection. Currently, *GET*, *HEAD*, *POST*, *PUT* and *DELETE* requests are supported:

```
$response = $httpUri->connect()->get();
$response = $httpUri->connect()->head();
$response = $httpUri->connect()->post($postBody);
$response = $httpUri->connect()->put($putBody);
$response = $httpUri->connect()->delete();
```

For *POST* and *PUT* there is one required parameter which should contain the post/put body. For *POST* alternatively an associative `array` can be supplied which will be transformed into form post values, which will lead to an automatically added *Content-type: application/x-www-form-urlencoded* header to the request.

The response can then be read. It provides access to all single details of the HTTP response.

Socket operations
-----------------

[](#socket-operations)

Socket operations can be done using `stubbles\peer\Socket`. A socket can be created by supplying the host to the constructor, and optionally a port. If no port is specified it will fall back to port 80.

On construction only the socket instance is created. To actually open the connection the `connect()` method must be called. Optionally a time-out for establishing the connection can be supplied, if none given its 2 seconds. When succesfully established, it returns a `stubbles\peer\Stream` instance which provides methods to read from and write to the socket.

Other utility classes
---------------------

[](#other-utility-classes)

### `stubbles\peer\HeaderList`

[](#stubblespeerheaderlist)

This class provides possibilities to work with headers, mainly parsing a string which contains headers and maintaining a list of headers.

### `stubbles\peer\http\AcceptHeader`

[](#stubblespeerhttpacceptheader)

This class can parse the [accept header](http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html)from HTTP and provides access to check if a certain type is accepted, what it's priority is, and to find the best match if the accept header is compared against a selection of types. It can cope with *Accept*, *Accept-Charset* and *Accept-Encoding*.

### `stubbles\peer\ParsedUri`

[](#stubblespeerparseduri)

Takes a uri string as construction argument and provides access to each part of the uri. In constrast to `stubbles\peer\Uri` and `stubbles\peer\http\HttpUri` no checks are done on the url string which means you can construct instances from invalid url strings, which is not possible with both other classes.

### `stubbles\peer\QueryString`

[](#stubblespeerquerystring)

Takes a query string as construction argument and provides access to all of the parameters within the query string to modify and remove them or to add other parameters; and to rebuild a complete query string from this.

### `stubbles\peer\IpAddress`

[](#stubblespeeripaddress)

*Available since release 4.0.0*

Represents an ip address and possible operations on an ip address.

Integration with *stubbles/values*
----------------------------------

[](#integration-with-stubblesvalues)

In case the package *stubbles/values* is present a recognition for `stubbles\values\Parse` to parse http URIs to instances of `stubbles\peer\http\HttpUri` will automatically be added.

Also, some checks are added to `stubbles\values\Value` (*available since release 7.1.0*):

- `isMailAddress()`
- `isIpAddress()`
- `isIpV4Address()`
- `isIpV6Address()`
- `isHttpUri()`
- `isExistingHttpUri()`

###  Health Score

53

—

FairBetter than 97% of packages

Maintenance82

Actively maintained with recent releases

Popularity18

Limited adoption so far

Community14

Small or concentrated contributor base

Maturity84

Battle-tested with a long release history

 Bus Factor1

Top contributor holds 70.3% 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 ~401 days

Recently: every ~545 days

Total

10

Last Release

161d ago

Major Versions

v7.1.0 → v8.0.02016-07-26

v8.1.0 → v9.0.02019-10-30

v9.0.2 → v10.0.02023-12-26

v10.0.1 → v11.0.02025-11-29

PHP version history (5 changes)v7.0.0PHP &gt;=5.6.0

v8.0.0PHP ^7.0

v9.0.0PHP ^7.3

v10.0.0PHP ^8.2

v11.0.0PHP ^8.3

### Community

Maintainers

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

---

Top Contributors

[![mikey179](https://avatars.githubusercontent.com/u/190475?v=4)](https://github.com/mikey179 "mikey179 (52 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (22 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/stubbles-peer/health.svg)

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

PHPackages © 2026

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