定制 slick/http 二次开发

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

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

slick/http

Composer 安装命令:

composer require slick/http

包简介

Slick/Http is an useful library for Web application foundation. It implements the PSR-7 message interface and has a middleware server, session handling and an HTTP client.

README 文档

README

Latest Version on Packagist Software License Quality Score Total Downloads

slick/http provides a complete implementation of PSR-7 (HTTP Message Interface) and PSR-18 (HTTP Client Interface), enabling developers to work seamlessly with HTTP messages and clients in a standardized way. By adhering to these PSR standards, the library ensures interoperability with other libraries and frameworks that follow the same specifications, promoting code reusability and consistency across projects. In addition, the library includes an HTTP server that functions as a middleware runner, allowing you to pass a collection of middleware objects to the server, which are called in sequence to compose a response for the current server request. This middleware approach provides a flexible, modular way to handle HTTP requests, enhancing the scalability and maintainability of your PHP applications.

This package is compliant with PSR-2 code standards and PSR-4 autoload standards. It also applies the semantic version 2.0.0 specification.

Install

Via Composer

$ composer require slick/http

Usage

Server Request Message

This is an handy way to have a HTTP request message that has all necessary information that was sent by the client to the server.

Its very simple to get this:

use Slick\Http\Message\Server\Request;

$request = new Request();

The $request encapsulates all data as it has arrived at the application from the CGI and/or PHP environment, including:

  • The values represented in $_SERVER.
  • Any cookies provided (generally via $_COOKIE)
  • Query string arguments (generally via $_GET, or as parsed via parse_str())
  • Upload files, if any (as represented by $_FILES)
  • Deserialized body parameters (generally from $_POST)

Consider the following message:

POST /path?_goto=home HTTP/1.1
Host: www.example.org
Content-Type: application/x-www-form-urlencoded; charset=utf-8
Content-Lenght: 5
Authorization: Bearer PAOIPOI-ASD9POKQWEL-KQWELKAD==

foo=bar&bar=baz

Now lest retrieve its information using the $request object:

echo $request->getHeaderLine('Authorization');  // will print "Bearer PAOIPOI-ASD9POKQWEL-KQWELKAD=="

print_r($request->getParsedBody());
# Array (
#   [foo] => bar,
#   [bar] => baz
#)

print_r($request->getQueryParam());
# Array (
#   [_goto] => home
#)

HTTP Server

The HTTP server is a middleware runner. You can pass a collection of middleware objects to the server and they all will be called in their sequence in order to compose a response for current server request.

Lets create a simple web application that will print out a greeting to a query parameter named 'name'. Lets first create our middleware classes:

use Psr\Http\Server\MiddlewareInterface;
use Interop\Http\Server\RequestHandlerInterface;

class Greeting implements MiddlewareInterface
{
  
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler)
    {
        $params = $request->getQueryParams();
        $request = (isset($params['name'])
            ? $request->withAttribute('greeting', "Hello, {$params['name']}!")
            : $request;
        
        $response = $handler->handle($request);
        return $response;
    }
}

This middleware retrieves the query parameter with name and add an attribute to the request object passed to the next middleware in the stack. Lets create our printer:

use Psr\Http\Server\MiddlewareInterface;
use Interop\Http\Server\RequestHandlerInterface;

class Printer implements MiddlewateInterface
{
  
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler)
    {
      $greeting = $request->getAttribute('greeting', false);
      $text = $greeting ?: 'Hi!';
      
      $response = $handler->handle($request);
      
      $response->getBody()->write($text);
      
      return $response;
    }
}

Now lets create our main server application:

use Slick\Http\Server\MiddlewareStack;
use Slick\Http\Message\Response;
use Slick\Http\Message\Server\Request;

$stack = (new MiddlewareStack())
    ->push(new Greeting())
    ->push(new Printer())
    ->push(function () { return new Response(200); }); // Order matters!

$response = $stack->process(new Request);

// Emit headers iteratively:
foreach ($response->getHeaders() as $name => $values) {
    header(sprintf('%s: %s', $name, implode(', ', $value)), false);
}

// Print out the message body
echo $response->getBody();

HTTP Client

Working with HTTP requests (as a client) is one of the most common tasks nowadays. Almost every application need to retrieve data from a web service or API and therefore an HTTP client is a must have.

Slick\Http\Client\CurlHttpClient implments PSR-18 HTTP Client

public function sendRequest(RequestInterface $request): ResponseInterface;

It depends on PHP's cURL extension to connect and make HTTP requests. Lets see an example:

use Psr\Http\Message\ResponseInterface;
use Slick\Http\Client\CurlHttpClient;
use Slick\Http\Message\Request;
use Slick\Http\Message\Uri;

$request = new Request('GET', new Uri('https://example.com'));
$client = new CurlHttpClient();

$response = $client->sendRequest($request);

$data = json_decode($response->getBody()->getContents());

Session

Slick\Http\Session has a very simple and ease implementation. By default it will use the PHP session handling and it comes with a simple factory that you can use to create it:

use Slick\Http\Session;

$session = Session::create();
Write session data
$session->set('foo', 'bar');
Read session data
$session->read('foo', null); // if foo is not found the default (null) will be returned.
Deleting session data
$session->erase('foo');

You can create your own session drivers if you need to encrypt you data or change the save handler to save in a database by simply implementing the Slick\Http\Session\SessionDriverInterface. It also comes with a Slick\Http\Session\Driver\AbstractDriver class that has all the basic operations of the interface already implemented.

Please check documentation site for a complete reference.

Change log

Please see CHANGELOG for more information on what has changed recently.

Testing

$ composer test

Contributing

Please see CONTRIBUTING and CODE_OF_CONDUCT for details.

Security

If you discover any security related issues, please email slick.framework@gmail.com instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.

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

slick/http 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 7.96k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2016 年 01 月 30 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 7.96k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 22
  • 依赖项目数: 6
  • 推荐数: 0

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2016-01-30