masterklavi / phprequest
PHP 请求
Requires
- php: >=5.4.0
- ext-curl: *
README
它包含一些函数,可以轻松地进行请求和解析数据。
示例
use \phprequest\Request; print Request::request('https://api.ipify.org/'); // prints your IP address received from https://api.ipify.org/ print PHP_EOL; print Request::get('https://api.ipify.org?format=json', ['filter' => 'json'])->ip . PHP_EOL; print Request::get('https://api.ipify.org?format=json', ['filter' => 'json_assoc'])['ip'] . PHP_EOL; // Request::post('http://example.com', ['data' => ['name' => 'John']]); // Request::multi(['http://example.com', 'http://example.com/1', 'http://example.com/2'], ['concurrency' => 2]); // Request::multiPost(['http://example.com', ['http://example.com/1', ['filter' => 'json']], 'http://example.com/2'], ['concurrency' => 2]);
要求
- PHP 版本 5.4.0 或更高
- PHP 扩展
ext-curl
已启用
安装
使用 Composer
获取包
$ composer require masterklavi/phprequest
包含 vendor/autoload.php
include 'vendor/autoload.php'; use \phprequest\Request; $data = Request::get('http://www.cbr-xml-daily.ru/daily_json.js', ['filter' => 'json']); print 'USD: ' . $data->Valute->USD->Value, PHP_EOL;
手动安装
克隆 git 仓库
$ git clone https://github.com/masterklavi/phprequest.git
或从 https://github.com/masterklavi/phprequest/archive/master.zip 下载包
包含 autoload.php
<?php include 'autoload.php'; use \phprequest\Request; $data = Request::get('http://www.cbr-xml-daily.ru/daily_json.js', ['filter' => 'json']); print 'USD: ' . $data->Valute->USD->Value, PHP_EOL;
方法
Request::request
请求给定的 URL。
语法
mixed Request::request(string $url, array $options = [])
参数
$url
– URL 地址
$options
– 请求选项数组
返回值
默认情况下返回响应(正文)作为 string
。当设置 filter
选项时返回 mixed
值。失败时返回 false
。
Request::get
Request::post
等同于 Request::request($url, ['method' => 'POST'])
Request::multi
请求给定的 URL(使用 curl multi 进行并行请求)。
语法
mixed Request::multi(string $urls, array $options = [])
参数
$url
– URL 地址字符串数组或 URL 地址和它们选项的数组(例如 [ 'http://a.ru', ['http://b.ru', ['filter' => 'json']] ]
)
$options
– 请求选项数组
返回值
为给定的 URL 返回一个结果数组。结果可能包含
- 默认情况下作为
string
的响应(正文) - 当设置
filter
选项时返回mixed
值 - 失败时返回
false
Request::multiGet
等同于 Request::multi()
Request::multiPost
等同于 Request::multi($urls, ['method' => 'POST'])
请求选项
curl 选项列表
特殊选项列表
过滤器
普通过滤器
Request::get($url, ['filter' => 'json']);
json
- 将正文解释为 json 并返回一个对象json_assoc
- 将正文解释为 json 并返回一个关联数组xml
- 将正文解释为 xml 并返回 SimpleXML 对象plain
- 返回完整的响应(包括头部)作为纯文本headers
- 返回一组头部headers_body
- 返回一组头部和正文作为纯文本
复杂过滤器
Request::get($url, ['filter' => ['filter_name', $option]])
-
cut
- 使用begin
和end
子串(标签)裁剪出新的子串
语法Request::get($url, ['filter' => ['cut', $options]])
$options
是键值数组,可能包含'begin' => 'some string'
- 裁剪开始的子串'end' => 'some string'
- 裁剪结束的子串'case_sensivity' => true
- 使用大小写敏感的 begin 和 end 作为子串'mbstring' => true
- 在裁剪时使用 mb_* 函数
-
regex
- 返回与正则表达式匹配的结果数组(使用preg_match
)
语法Request::get($url, ['filter' => ['cut', '#reg.exp. pattern#']])
如果
pattern
不匹配正文,则返回null
,如果发生错误,则返回false
-
regex_one
- 返回第一个匹配的文本(使用preg_match
) -
regex_all
- 返回使用正则表达式搜索的结果数组数组(使用preg_match_all
) -
regex_set
- 返回使用正则表达式搜索的结果数组数组(使用具有顺序的preg_match_all
) -
regex_col
- 返回匹配正则表达式的第一个值的结果数组(使用preg_match_all
)