中微子 / http
中微子 Http 包。
0.1.0-RC2
2017-05-02 13:03 UTC
Requires
- php: >=5.6
Requires (Dev)
- phpunit/phpunit: ~5.7
- satooshi/php-coveralls: ~1.0
This package is auto-updated.
Last update: 2024-09-20 14:04:45 UTC
README
使用 Curl 或 HttpStream 的 HttpClient 库。
基本
$provider->get($url, $parameters, $options); $provider->post($url, $parameters, $options); $provider->delete($url, $parameters, $options); $provider->put($url, $parameters, $options); $provider->head($url, $parameters, $options); $provider->patch($url, $parameters, $options); $provider->request($method, $url, $parameters, $options);
$url
包含要调用的 URL。 $parameters
包含要发送的参数。 $options
包含请求的选项。
$options = [ // Headers to send 'headers' => [], // Retrieve the full response (Header + Body) 'full' => true, // Make a JsonRequest (Only for POST, PUT, PATCH methods) 'json' => true, ];
提供者
Curl
需要 curl 扩展。
如何使用
use \Neutrino\Http\Provider\Curl as HttpCurl; use \Neutrino\Http\Method; $curl = new HttpCurl; $response = $curl ->get('http://www.google.com', ['foo' => 'bar'], ['Accept' => 'text/plain']) ->send(); $response->code; // HTTP Status Code
Curl\Streaming
Curl\Stream 允许您通过部分恢复内容的方式来处理大量查询。
如何使用
use \Neutrino\Http\Provider\Curl\Streaming as HttpCurlStream; use \Neutrino\Http\Method; $curl = new HttpCurlStream; $response = $curl ->get('http://www.google.com') ->on(HttpCurlStream::EVENT_START, function (HttpCurlStream $curl) { // Start to download response body // Header are fully loaded when the event are raised }) ->on(HttpCurlStream::EVENT_PROGRESS, function (HttpCurlStream $curl, $content) { // Download progress // $content contain the response part }) ->send();
传输大量数据,而不会超过 PHP 内存限制
$curl ->get('http://www.google.com') ->on(HttpCurlStream::EVENT_START, function (HttpCurlStream $curl) { if ($curl->getResponse()->header->has('Content-Length')) { header('Content-Length: ' . $curl->getResponse()->header->get('Content-Length')); } }) ->on(HttpCurlStream::EVENT_PROGRESS, function (HttpCurlStream $curl, $content) { echo $content; ob_flush(); flush(); // => Direct echo contents & flush the output (free memory) }) ->send();
下载大量文件,而不会超过 PHP 内存限制
$resource = fopen($path, 'w'); $curl ->get('http://www.google.com') ->on(HttpCurlStream::EVENT_PROGRESS, function (HttpCurlStream $curl, $content) use ($resource) { fwrite($resource, $content, strlen($content)); }) ->send(); fclose($resource);
StreamContext
StreamContext 通过 PHP 包装器执行 HTTP 调用。
这需要您将 "allow_url_fopen" 配置值设置为 '1'。
如何使用
use \Neutrino\Http\Provider\StreamContext as HttpStreamCtx; use \Neutrino\Http\Method; $streamCtx = new HttpStreamCtx; $response = $streamCtx ->get('http://www.google.com', ['foo' => 'bar'], ['headers' => ['Accept' => 'text/plain']]) ->send(); $response->code; // HTTP Status Code
StreamContext\Streaming
类似于 Curl\Streaming,StreamContext\Streaming 允许您通过部分恢复内容的方式来处理大量查询。
如何使用
use \Neutrino\Http\Provider\StreamContext\Streaming as HttpStreamCtxStreaming; use \Neutrino\Http\Method; $streamCtx = new HttpStreamCtxStreaming; $response = $streamCtx ->get('http://www.google.com') ->on(HttpStreamCtxStreaming::EVENT_START, function (HttpStreamCtxStreaming $streamCtx) { // Start to download response body // Header are fully loaded when the event are raised }) ->on(HttpStreamCtxStreaming::EVENT_PROGRESS, function (HttpStreamCtxStreaming $streamCtx, $content) { // Download progress // $content contain the response part }) ->send();
认证
认证是请求的一个组件。
Auth\Basic
Auth\Basic 提供配置带有基本认证的调用的元素。
如何使用
use \Neutrino\Http\Auth\Basic as AuthBasic; use \Neutrino\Http\Provider\StreamContext as HttpStreamCtx; use \Neutrino\Http\Method; $streamCtx = new HttpStreamCtx; $response = $streamCtx ->get('http://www.google.com') ->setAuth(new AuthBasic('user', 'pass')) ->send();
Auth\Curl
针对 Curl 提供者。
Auth\Curl 提供构建带有 Curl 认证的调用的元素。
如何使用
use \Neutrino\Http\Auth\Curl as AuthCurl; use \Neutrino\Http\Provider\Curl as HttpCurl; use \Neutrino\Http\Method; $curl = new HttpCurl; $response = $curl ->get('http://www.google.com') ->setAuth(new AuthCurl(CURLAUTH_BASIC | CURLAUTH_DIGEST, 'user', 'pass')) ->send();
自定义认证组件
您可以轻松创建自己的认证组件
namespace MyLib\Http\Auth; use Neutrino\Http\Request; use Neutrino\Http\Contract\Request\Component; class Hmac implements Component { private $id; private $value; public function __construct($id, $value) { $this->id = $id; $this->value = $value; } public function build(Request $request) { $date = date('D, d M Y H:i:s', time()); $signature = urlencode(base64_encode(hash_hmac('sha1', "date: $date", $this->value, true))); $request ->setHeader('Date', $date) ->setHeader('Authorization', 'Signature keyId="' . $this->id . '",algorithm="hmac-sha1",signature="' . $signature . '"'); } }
use \MyLib\Http\Auth\Hmac as AuthHmac; use \Neutrino\Http\Provider\Curl as HttpCurl; use \Neutrino\Http\Method; $curl = new HttpCurl; $response = $curl ->get('http://www.google.com') ->setAuth(new AuthHmac('key_id', 'key_value')) ->send();
响应
基本
$response->code; // HTTP Status Code $response->status; // HTTP Status Message $response->header; // Response Headers $response->body; // Response Body
提供者信息
$response->errorCode; // Provider Error Code $response->error; // Provider Error Message $response->providerDatas; // All Provider Information (if available)
解析
use \Neutrino\Http\Parser; // Json Body => Object $jsonObject = $response->parse(Parser\Json::class)->data; // Xml Body => SimpleXMLElement $xmlElement = $response->parse(Parser\Xml::class)->data; // Xml Body => array $xmlArray = $response->parse(Parser\XmlArray::class)->data; // Other exemple : (PHP7) $response->parse(new class implements Parser\Parserize { public function parse($body) { return unserialize($body); } }); $response->data; // Unserialized body