ehough / shortstop
此包已被弃用且不再维护。未建议替代包。
适用于 PHP 5.2+ 的快速灵活的 HTTP 客户端
2.0.2
2013-07-17 22:09 UTC
Requires
- php: >=5.2
- ehough/chaingang: ~1.0.0
- ehough/curly: ~1.0.0
- ehough/epilog: ~1.5
- ehough/tickertape: ~2.3
Requires (Dev)
- ehough/mockery: ~0.8
- phpunit/phpunit: 3.7.*
This package is not auto-updated.
Last update: 2020-01-24 14:52:03 UTC
README
此库已不再维护。适用于 PHP 5.2+ 的快速灵活的 HTTP 客户端。
动机
今天我们可以从众多优秀的 PHP HTTP 客户端库中选择。然而,它们都假设底层 PHP 安装有特定的配置。例如,Guzzle 需要 cURL。如果你想要部署可以在任意 PHP 服务器上运行的代码,这会带来问题。
shortstop
通过动态选择最适合服务器环境的底层 HTTP 机制来缓解这个问题。因此,shortstop
几乎可以在任何 PHP 5.2+ 安装上工作。
特性
- 兼容 PHP 5.2+
- 支持 cURL、PHP 的 HTTP 扩展、fsockopen()、fopen() 和 流
- 仅支持 HTTP 1.0(目前如此!)
- 强大的 HTTP 内容压缩支持。具体机制(deflate、gzip 等)由服务器环境确定
- 内置分块传输解码
- 通过兼容 Symfony 事件派发器的 事件派发器 进行高度可配置和可扩展
- 经过大量测试,代码覆盖率优秀
使用方法
如您所见,stash
所有的灵活性代价是组装组件可能相当冗长。使用控制反转容器可能会有所帮助,因为实际的客户端可以作为单例重用。但是,如果你想要单独使用 stash
,可以按照以下代码操作。
//build an event dispatcher $eventDispatcher = new ehough_tickertape_EventDispatcher(); //implements ehough_tickertape_EventDispatcherInterface //build an HTTP message parser $httpMessageParser = new ehough_shortstop_impl_exec_DefaultHttpMessageParser(); //build a chain of HTTP transport mechanisms $chain = new ehough_chaingang_impl_StandardChain(); $chain->addCommand(new ehough_shortstop_impl_exec_command_CurlCommand($httpMessageParser, $eventDispatcher)); $chain->addCommand(new ehough_shortstop_impl_exec_command_ExtCommand($httpMessageParser, $eventDispatcher)); $chain->addCommand(new ehough_shortstop_impl_exec_command_FopenCommand($httpMessageParser, $eventDispatcher)); $chain->addCommand(new ehough_shortstop_impl_exec_command_FsockOpenCommand($httpMessageParser, $eventDispatcher)); $chain->addCommand(new ehough_shortstop_impl_exec_command_StreamsCommand($httpMessageParser, $eventDispatcher)); //build the HTTP client $client = new ehough_shortstop_impl_DefaultHttpClient($eventDispatcher, $chain); //build a chain of HTTP content decoders $contentDecodingChain = new ehough_chaingang_impl_StandardChain(); $contentDecodingChain->addCommand(new ehough_shortstop_impl_decoding_content_command_NativeGzipDecompressingCommand()); $contentDecodingChain->addCommand(new ehough_shortstop_impl_decoding_content_command_SimulatedGzipDecompressingCommand()); $contentDecodingChain->addCommand(new ehough_shortstop_impl_decoding_content_command_NativeDeflateRfc1950DecompressingCommand()); $contentDecodingChain->addCommand(new ehough_shortstop_impl_decoding_content_command_NativeDeflateRfc1951DecompressingCommand()); //build an HTTP content decoder $httpContentDecoder = new ehough_shortstop_impl_decoding_content_HttpContentDecodingChain($contentDecodingChain); //add some HTTP request listeners $eventDispatcher->addListener(ehough_shortstop_api_Events::REQUEST, array(new ehough_shortstop_impl_listeners_request_RequestDefaultHeadersListener($httpContentDecoder), 'onPreRequest')); $eventDispatcher->addListener(ehough_shortstop_api_Events::REQUEST, array(new ehough_shortstop_impl_listeners_request_RequestLoggingListener(), 'onPreRequest')); //build a chain of HTTP transport decoders $transferDecodingChain = new ehough_chaingang_impl_StandardChain(); $transferDecodingChain->addCommand(new ehough_shortstop_impl_decoding_transfer_command_ChunkedTransferDecodingCommand()); //build an HTTP transport decoder $httpTransferDecoder = new ehough_shortstop_impl_decoding_transfer_HttpTransferDecodingChain($transferDecodingChain); //add some HTTP response listeners $eventDispatcher->addListener(ehough_shortstop_api_Events::RESPONSE, array(new ehough_shortstop_impl_listeners_response_ResponseDecodingListener($httpTransferDecoder, 'Transfer'), 'onResponse')); $eventDispatcher->addListener(ehough_shortstop_api_Events::RESPONSE, array(new ehough_shortstop_impl_listeners_response_ResponseDecodingListener($httpContentDecoder, 'Content'), 'onResponse')); $eventDispatcher->addListener(ehough_shortstop_api_Events::RESPONSE, array(new ehough_shortstop_impl_listeners_response_ResponseLoggingListener(), 'onResponse')); //build a new HTTP request $request = new ehough_shortstop_api_HttpRequest('GET', 'https://github.com/'); //execute the request $response = $client->execute($request); $status = $response->getStatusCode(); //e.g. 200 $entity = $response->getEntity(); // instance of ehough_shortstop_api_HttpEntity $type = $entity->getContentType(); // e.g. text/html $body = $entity->getContent(); // the actual body of the response