onoi / http-request
一个简约的 http/curl 请求接口库
1.3.1
2017-01-14 16:25 UTC
Requires
- php: >=5.3.2
- onoi/cache: ~1.2
Suggests
- lib-curl: Allows making CURL requests
README
这是一个简约的 http/curl 请求接口,曾是 Semantic MediaWiki 代码库的一部分,现在作为一个独立的库进行部署。
该库提供以下功能:
HttpRequest接口CurlRequest作为HttpRequest的 cURL 实现CachedCurlRequest以支持对重复的CurlRequest请求进行低级缓存MultiCurlRequest以利用 cURL 多线程功能SocketRequest以创建异步套接字连接
要求
- PHP 5.3 或更高版本
安装
该库推荐通过将依赖项添加到您的 composer.json 来安装。
{
"require": {
"onoi/http-request": "~1.3"
}
}
使用
use Onoi\HttpRequest\CurlRequest; use Onoi\HttpRequest\Exception\BadHttpResponseException; use Onoi\HttpRequest\Exception\HttpConnectionException; class Foo { private $curlRequest = null; public function __constructor( CurlRequest $curlRequest ) { $this->curlRequest = $curlRequest; } public function doMakeHttpRequestTo( $url ) { $this->curlRequest->setOption( CURLOPT_URL, $url ); if ( !$this->curlRequest->ping() ) { throw new HttpConnectionException( "Couldn't connect" ); } $this->curlRequest->setOption( CURLOPT_RETURNTRANSFER, true ); $this->curlRequest->setOption( CURLOPT_HTTPHEADER, array( 'Accept: application/x-turtle' ) ); $response = $this->curlRequest->execute(); if ( $this->curlRequest->getLastErrorCode() == 0 ) { return $response; } throw new BadHttpResponseException( $this->curlRequest ); } }
$httpRequestFactory = new HttpRequestFactory(); $instance = new Foo( $httpRequestFactory->newCurlRequest() ); $response = $instance->doMakeHttpRequestTo( 'http://example.org' ); OR $cacheFactory = new CacheFactory(); $compositeCache = $cacheFactory->newCompositeCache( array( $cacheFactory->newFixedInMemoryLruCache( 500 ), $cacheFactory->newDoctrineCache( new \Doctrine\Common\Cache\RedisCache() ) ) ); $httpRequestFactory = new HttpRequestFactory( $compositeCache ); $cachedCurlRequest = $httpRequestFactory->newCachedCurlRequest(); // Responses for a request with the same signature (== same endpoint and same query // content) will be cached if the request was successful for a specified 1 h (3600 sec) $cachedCurlRequest->setOption( ONOI_HTTP_REQUEST_RESPONSECACHE_TTL, 60 * 60 ); $instance = new Foo( $cachedCurlRequest ); $response = $instance->doMakeHttpRequestTo( 'http://example.org' );
贡献和支持
如果您想为该项目做出贡献,请订阅开发者邮件列表,并查看 贡献指南。过去做出过贡献的人名单可以在 这里 找到。
测试
该库提供了单元测试,涵盖通常由 持续集成平台 运行的核心功能。测试也可以使用根目录中找到的 PHPUnit 配置文件手动执行。
发行说明
- 1.3.1 (2016-01-14)
- 扩展
SocketRequest以匹配可能的 TLS 端口
- 1.3.0 (2015-11-23)
- 弃用
CachedCurlRequest::setCachePrefix和CachedCurlRequest::setExpiryInSeconds,并改用通过选项ONOI_HTTP_REQUEST_RESPONSECACHE_PREFIX和ONOI_HTTP_REQUEST_RESPONSECACHE_TTL设置(任何过期更改都将自动使现有缓存中的项目无效) - 弃用
CachedCurlRequest::isCached,并改用CachedCurlRequest::isFromCache
- 1.2.0 (2015-11-09)
- 将 "wasAccepted" 添加到
SocketRequest响应输出 - 添加选项
ONOI_HTTP_REQUEST_FOLLOWLOCATION以支持在SocketRequest::ping请求期间,在遇到301HTTP 响应时重置 URL 位置
- 1.1.0 (2015-09-12)
- 将
AsyncCurlRequest重命名为MultiCurlRequest - 弃用
MultiCurlRequest::setCallback,并将其替换为MultiCurlRequest::setOption( ONOI_HTTP_REQUEST_ON_COMPLETED_CALLBACK, ... ) - 添加
SocketRequest以创建异步套接字连接
- 1.0.0 (2015-07-22,首次发布)
- 添加了
HttpRequest接口 - 添加了
CurlRequest实现 - 添加了
CachedCurlRequest以扩展CurlRequest实现 - 添加了
AsyncCurlRequest实现