p3k/http
Composer 安装命令:
composer require p3k/http
包简介
A simple wrapper API around the PHP curl functions
README 文档
README
A simple HTTP client, used by https://p3k.io projects.
Usage
GET
$http = new p3k\HTTP(); $headers = [ 'Accept: text/html, */*' ]; $response = $http->get('http://example.com/', $headers);
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
$http = new p3k\HTTP(); $headers = [ 'Accept: text/html, */*' ]; $response = $http->head('http://example.com/', $headers);
Response
The get/post/head functions will return an array with the following properties:
code- integer, the HTTP response code that was returnedheaders- array, the HTTP headers returnedrels- array, the parsed HTTP rel values from anyLinkheadersbody- string, the body of the HTTP response, or false/omit for a HEAD requesterror- string, an error string. see below for the enumerated list.error_description- string,url- string, the final URL retrieved after following any redirectsdebug- string, the full HTTP response
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] => <https://switchboard.p3k.io/>; rel="hub"
[1] => <https://aaronparecki.com/auth>; rel="authorization_endpoint"
[2] => <https://aaronparecki.com/micropub>; rel="micropub"
[3] => <https://aaronparecki.com/auth/token>; rel="token_endpoint"
[4] => <https://aaronparecki.com/>; 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
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
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
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
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 https://github.com/aaronpk/XRay/tree/master/tests for an example of using this transport.
License
Copyright 2017 by Aaron Parecki
Available under the MIT license.
p3k/http 适用场景与选型建议
p3k/http 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 27.65k 次下载、GitHub Stars 达 14, 最近一次更新时间为 2017 年 04 月 28 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 p3k/http 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 p3k/http 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 27.65k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 14
- 点击次数: 7
- 依赖项目数: 4
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2017-04-28