承接 stubbles/peer 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

stubbles/peer

Composer 安装命令:

composer require stubbles/peer

包简介

Help with socket operations.

README 文档

README

Help with socket operations.

Build status

Tests

Latest Stable Version Latest Unstable Version

Installation

stubbles/peer is distributed as Composer package. To install it as a dependency of your package use the following command:

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

Requirements

stubbles/peer requires at least PHP 8.3.

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.

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

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. 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

  • 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

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

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

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

stubbles\peer\HeaderList

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

This class can parse the accept header 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

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

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

Available since release 4.0.0

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

Integration with stubbles/values

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()

stubbles/peer 适用场景与选型建议

stubbles/peer 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2.84k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2016 年 01 月 03 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 stubbles/peer 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 stubbles/peer 我们能提供哪些服务?
定制开发 / 二次开发

基于 stubbles/peer 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 2.84k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 35
  • 依赖项目数: 2
  • 推荐数: 1

GitHub 信息

  • Stars: 0
  • Watchers: 2
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: BSD-3-Clause
  • 更新时间: 2016-01-03