claudio-silva / http-client
Http 客户端
Requires
- php: >=5.4.0
This package is auto-updated.
Last update: 2024-09-12 23:10:45 UTC
README
这是一个非常轻量级的 PHP 5.4+ 库,用于发送 HTTP 请求,具有流畅的 API。
运行时要求
- PHP >= 5.4
- CURL PHP 扩展(大多数 PHP 安装已包含)
安装
首选的安装方法是使用Composer。
在命令行中,在您的项目文件夹中输入
composer require claudio-silva/http-client
示例
use Http; $req = new Client ('http://some.net/api');
最简单的情况:获取一个 text/html 页面
$text = $req->get ('index.html')->send ();
可以轻松地发出更复杂的请求
// Get http://some.net/api/books?id=1 $xml = $req ->get ('books') ->param ('id', 1) ->expectXml () ->header ('X-Custon', 'test') ->send (); // Get http://some.net/api/books/author1/id1?page=1&filter=john $book = $req ->get ('books/%s/%s', 'author1', 'id1') ->expectJson () ->params ([ 'page' => 1, 'filter' => 'john' ]) ->headers ([ 'Header-One' => 'some value', 'Header-Two' => 'some value' ]) ->send ();
完全 REST
$req->post ('authors/%s/posts', $author)->with ($data)->send (); $req->put ('authors/%s/posts/%s', $author, $id)->with ($data)->send (); $req->delete ('posts/%s', $id)->send ();
如果需要,提供完全控制
$req->autoCheck = false; //throw no exceptions $req->referrer = 'http://google.com'; $req->cookieJar ['MyCookie'] = 'my value'; $req->method ('get') ->url ('books/%s/%s', $name, $id) ->header ('Accept', 'application/json') ->transform (function ($response) { return json_decode ($response); }) ->send (); if ($req->responseStatus != 200) throw new HttpException ("Can't load book", $req->responseStatus); echo "My session: " . $req->responseCookies['PHPSESSID']; $book = $req->responseBody; ++$book->reads; // Begin a new request, on the same browsing context (referrer and cookies are automatically set) $req ->begin () ->put ('books/%s/%s', $name, $id) ->with ($book) ->send ();
API
baseUrl ($url)
设置后续请求的 URL 前缀。
@param string $url
@return ClientInterface
如果不存在,它会追加一个尾部斜杠。
begin ()
基于当前请求返回一个新的请求,同时保留当前的导航会话。
@return ClientInterface
基础 URL、Cookies 和引用者被保留。所有其他设置都初始化为默认值。
delete ($url)
通过将方法设置为 DELETE
并将其 URL 设置为指定的 URL 来开始一个新的请求。
@param string $url
@param string ...$args URL paramet
@return ClientInterface
它清除剩余的请求数据(参数、头信息等)。要发出一个新的请求并重用当前数据,请再次调用
send()
。
此方法支持参数化 URL(例如:动态 URL 段)。方法调用上剩余的任何参数都将注入到使用 sprintf
语法编写的占位符中。
要设置 URL 参数,请使用
param()
代替此方法。
例
$req->delete ('posts/%s', $id)->send ();
expectJson ($associative = false)
为后续请求设置 Accept
头为 JSON 并将响应转换器设置为 JSON 解析器。
@param bool $associative When `true`, returned objects will be converted into associative arrays.
@return ClientInterface
expectText ()
为所有后续请求设置 Accept
头为最常见的文本类型并将响应转换器设置为 null
。
@return ClientInterface
expectXml ($fullDOM = false, $options = 0)
为后续请求设置 Accept
头为 JSON 并将响应转换器设置为 XML 解析器。
@param bool $fullDOM When `true`, a DOMDocument object will be returned, SimpleXmlElement otherwise.
@param int $options Bitwise OR of the libxml option constants.
@return ClientInterface
`send()` 将返回 `DOMDocument | SimpleXmlElement | null| false`。
`null` 或 `false` 可能意味着文档无法解析。
get ($url)
通过将方法设置为 GET
并将其 URL 设置为指定的 URL 来开始一个新的请求。
@param string $url
@param string ...$args Remaining arguments are injected into the URL on placeholders with `sprintf` syntax.
@return ClientInterface
它清除剩余的请求数据(参数、头信息等)。要发出一个新的请求并重用当前数据,请再次调用
send()
。
此方法支持参数化 URL(例如:动态 URL 段)。方法调用上剩余的任何参数都将注入到使用 sprintf
语法编写的占位符中。
要设置 URL 参数,请使用
param()
代替此方法。
例
$req->get ('api/%s/%s', $name, $id)->send ();
header ($name, $value)
向当前请求添加一个头信息。
@param string|string[] $name
@param string|int|float|null $value If null, the header will be removed.
@return ClientInterface
headers (array $map)
向当前请求添加多个头信息。
@param array $map A map of header names to header values.
@return ClientInterface
@see HttpRequestInterface::param()
method ($verb)
设置请求的 HTTP 动词。
@param string $verb One of `get|put|post|delete|patch|head|options|connect|trace`.
@return ClientInterface
param ($name, $value)
向当前请求添加一个 URL 参数。
@param string $name
@param string|int|float $value
@return ClientInterface
允许有相同名称的多个参数。
空值将导致参数不被添加。
空字符串将添加一个空值的参数。
params (array $map)
向当前请求添加多个 URL 参数。
@param array $map A map of parameter names to parameter values.
@return ClientInterface
@see HttpRequestInterface::param()
post ($url)
通过将方法设置为 POST
并将其 URL 设置为指定的 URL 来开始一个新的请求。
@param string $url
@param string ...$args Remaining arguments are injected into the URL on placeholders with `sprintf` syntax.
@return ClientInterface
它清除剩余的请求数据(参数、头信息等)。要发出一个新的请求并重用当前数据,请再次调用
send()
。
此方法支持参数化 URL(例如:动态 URL 段)。方法调用上剩余的任何参数都将注入到使用 sprintf
语法编写的占位符中。
要设置 URL 参数,请使用
param()
代替此方法。
例
$req->post ('authors/%s/posts', $author)->with ($data)->send ();
put ($url)
通过将方法设置为 PUT
并将其 URL 设置为指定的 URL 来开始一个新的请求。
@param string $url
@param string ...$args Remaining arguments are injected into the URL on placeholders with `sprintf` syntax.
@return ClientInterface
它清除剩余的请求数据(参数、头信息等)。要发出一个新的请求并重用当前数据,请再次调用
send()
。
此方法支持参数化 URL(例如:动态 URL 段)。方法调用上剩余的任何参数都将注入到使用 sprintf
语法编写的占位符中。
要设置 URL 参数,请使用
param()
代替此方法。
例
$req->put ('authors/%s/posts/%s', $author, $id)->with ($data)->send ();
send ()
执行当前链式请求并返回响应文本。
@return mixed The server's response body, eventually transformed.
此方法不会设置
Accept
头。您应通过expectXXX()
方法预先设置它。
transform ($callback)
设置响应转换器函数。
@param callable $callback A function to transform the response. It receives the raw response text as argument.
@return ClientInterface
url ($url)
设置请求的 URL。
@param string $url
@param string ...$args Remaining arguments are injected into the URL on placeholders with `sprintf` syntax.
@return ClientInterface
此方法支持参数化 URL(例如:动态 URL 段)。方法调用上剩余的任何参数都将注入到使用 sprintf
语法编写的占位符中。
要设置 URL 参数,请使用
param()
代替此方法。
例
$req->method ('get')->url ('api/%s/%s', $name, $id)->send ();
with ($body, $type = null)
设置 POST 或 PUT 请求的请求负载。
@param mixed $body The data to be sent with the request.
@param string $type One of `json|form|text|xml`. The appropriate `Content-Type` header will be added. If not
specified, no header will be set and the body will not be serialized.
@return ClientInterface
根据 $type
参数,将相应地序列化负载。
许可证
HTTP 客户端库是开源软件,受MIT 许可协议许可。
版权所有 ©2015 克劳迪奥·曼努埃尔·布拉斯·达·席尔瓦