定制 neutrino/http 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

neutrino/http

Composer 安装命令:

composer require neutrino/http

包简介

The Neutrino Http package.

README 文档

README

Build Status Coverage Status

Http Client library using Curl or HttpStream.

Basic

$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 Contain the url to call. $parameters Contain the parameters to send. $options Contain the options of the request.

$options = [
    // Headers to send
    'headers' => [],
    // Retrieve the full response (Header + Body)
    'full' => true,
    // Make a JsonRequest (Only for POST, PUT, PATCH methods)
    'json' => true,
];

Provider

Curl

require curl extension.

How use :

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 allows you to work with large queries, by recovers content part by part.

How use :

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();

Transfer huge data, without overloading the php memory :

$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();

Download huge file, without overloading the php memory :

$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 make HTTP call via the php wrapper.

This require you have "allow_url_fopen" configuration value set to '1'.

How use :

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

Such as Curl\Streaming, StreamContext\Streaming allows you to work with large queries, by recovers content part by part.

How use :

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

Authentication is a request component.

Auth\Basic

Auth\Basic provides the elements to configure a call with an Basic Authorization.

How use :

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

Specific for Curl provider.

Auth\Curl provides the elements to build a call with Curl Auth.

How use :

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();

Custom Auth Component

You can easily make your own Auth Component :

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

Basic

$response->code;   // HTTP Status Code
$response->status; // HTTP Status Message
$response->header; // Response Headers
$response->body;   // Response Body

Provider Info

$response->errorCode; // Provider Error Code
$response->error;     // Provider Error Message
$response->providerDatas; // All Provider Information (if available)

Parse

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

neutrino/http 适用场景与选型建议

neutrino/http 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 10 次下载、GitHub Stars 达 1, 最近一次更新时间为 2017 年 04 月 28 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「curl」 「json」 「xml」 「http」 「rest」 「http client」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 neutrino/http 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 neutrino/http 我们能提供哪些服务?
定制开发 / 二次开发

基于 neutrino/http 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 10
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 12
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2017-04-28