pear / http_request2
提供了一种简单执行HTTP请求的方法。
v2.6.0
2023-11-01 19:51 UTC
Requires
- php: >=5.6.0
- pear/net_url2: ^2.2.0
- pear/pear_exception: ^1.0.0
Requires (Dev)
- yoast/phpunit-polyfills: ^1.0.0
Suggests
- ext-curl: Allows using cURL as a request backend.
- ext-fileinfo: Adds support for looking up mime-types using finfo.
- ext-openssl: Allows handling SSL requests when not using cURL.
- ext-zlib: Allows handling gzip compressed responses.
README
提供了一种简单执行HTTP请求的方法,使用可插拔适配器
- Socket:HTTP协议的纯PHP实现(不使用http流包装器),基于旧的PEAR HTTP_Request包
- Curl:围绕PHP的cURL扩展的包装器
- Mock:用于测试依赖于HTTP_Request2的包,返回预定义的响应,无需网络交互
Socket和Curl适配器都支持带有数据和文件上传的POST请求、基本和摘要认证、cookies、跨请求管理cookies、HTTP和SOCKS5代理、gzip和deflate编码、重定向、使用观察者监控请求进度...
此包是PEAR HTTP_Request2,并已从PEAR SVN迁移过来
请通过GitHub问题报告所有问题。
欢迎提交拉取请求。
安装
可以使用PEAR或composer安装此包
$ pear install HTTP_Request2
或使用composer
$ composer require pear/http_request2
自2.4版发布以来,composer安装完全依赖于自动加载,不包含require_once
调用或使用include-path
选项。
基本用法
require_once 'HTTP/Request2.php'; // Only when installed with PEAR $request = new HTTP_Request2('http://pear.php.net/', HTTP_Request2::METHOD_GET); try { $response = $request->send(); if (200 == $response->getStatus()) { echo $response->getBody(); } else { echo 'Unexpected HTTP status: ' . $response->getStatus() . ' ' . $response->getReasonPhrase(); } } catch (HTTP_Request2_Exception $e) { echo 'Error: ' . $e->getMessage(); }
文档
...在PEAR网站上提供
当前版本的生成API文档也在这里。
测试、打包和安装(Pear)
要测试,在用composer安装依赖项后运行
$ phpunit tests/
您还可以使用以下命令测试已安装的包
$ phpunit [PEAR tests dir]/HTTP_Request2
由于PEAR包需要重新启用其require_once
语句,请在打包和安装之前运行辅助文件
$ php pear-package-helper.php
然后,要构建,只需简单
$ pear package .pear-package/package.xml
从头开始安装
$ pear install .pear-package/package.xml
升级
$ pear upgrade -f .pear-package/package.xml