binsoul/net-http-request 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

binsoul/net-http-request

Composer 安装命令:

composer require binsoul/net-http-request

包简介

Extended ServerRequest class and request factory

README 文档

README

Latest Version on Packagist Software License Total Downloads

This package provides a Request class which extends the PSR-7 ServerRequest class with header and role objects and simplifies access to attributes, query and post parameters.

The included RequestFactory class can build requests automatically from PHP superglobals or from given data. It uses the factory interfaces defined in binsoul/net-http-message-bridge to build URIs and streams. Headers typically set by load balancers or proxies are used to build requests.

Install

Via composer:

$ composer require binsoul/net-http-request

Usage

Build a request from PHP superglobals:

<?php

use BinSoul\Bridge\Http\Message\DefaultStreamFactory;
use BinSoul\Bridge\Http\Message\DefaultUriFactory;
use BinSoul\Net\Http\Request\RequestFactory;

require 'vendor/autoload.php';

$factory = new RequestFactory(new DefaultUriFactory(), new DefaultStreamFactory());
$request = $factory->buildFromEnvironment();

Build a request from provided data:

<?php

use BinSoul\Bridge\Http\Message\DefaultStreamFactory;
use BinSoul\Bridge\Http\Message\DefaultUriFactory;
use BinSoul\Bridge\Http\Message\Stream as HttpStream;
use BinSoul\IO\Stream\Type\MemoryStream;
use BinSoul\Net\Http\Request\RequestFactory;

require 'vendor/autoload.php';

$stream = new HttpStream(new MemoryStream(), 'r+');
$stream->write('Hello world!');

$factory = new RequestFactory(new DefaultUriFactory(), new DefaultStreamFactory());
$request = $factory->buildFromData($stream, $_SERVER, $_GET, $_POST, $_COOKIE, $_FILES);

echo (string) $request->getBody(); // Hello world!

Use the URI implementation from league/uri:

<?php

use BinSoul\Bridge\Http\Message\DefaultStreamFactory;
use BinSoul\Bridge\Http\Message\UriFactory;
use BinSoul\Net\Http\Request\RequestFactory;
use League\Uri\Schemes\Http as HttpUri;

require 'vendor/autoload.php';

class LeagueUriFactory implements UriFactory
{
    public function build($uri)
    {
        return HttpUri::createFromString($uri);
    }
}

$factory = new RequestFactory(new LeagueUriFactory(), new DefaultStreamFactory());
$request = $factory->buildFromEnvironment();

Example

Output a HTML page with information about the current request:

<?php

use BinSoul\Bridge\Http\Message\DefaultStreamFactory;
use BinSoul\Bridge\Http\Message\DefaultUriFactory;
use BinSoul\Net\Http\Request\RequestFactory;

require 'vendor/autoload.php';

$factory = new RequestFactory(new DefaultUriFactory(), new DefaultStreamFactory());
$request = $factory->buildFromEnvironment();

// Roles
$client = $request->getClient();
$server = $request->getServer();

// Headers
$userAgent = $request->getUserAgent();
$cacheControl = $request->getCacheControl();
$acceptMediaType = $request->getAcceptMediaType();
$acceptEncoding = $request->getAcceptEncoding();
$acceptLanguage = $request->getAcceptLanguage();
$acceptCharset = $request->getAcceptCharset();

// make var_export HTML friendly
function dump($value) {
    return htmlentities(preg_replace("/\s+=>\s+/m", ' => ', var_export($value, true)));
}

?>

<!DOCTYPE html>
<html>
<head>
    <style type="text/css">
        h1 { font-size: 150%; margin: 1em 0 0.25em 0; border-bottom: 1px solid #888;}
        td { vertical-align: top; padding: 2px 6px; }
        td:first-child {white-space: nowrap;}
    </style>
</head>
<body>
<h1>Request</h1>
<table>
    <tr>
        <td>protocol:</td>
        <td>HTTP/<?= $request->getProtocolVersion() ?></td>
    </tr>
    <tr>
        <td>method:</td>
        <td><?= $request->getMethod() ?></td>
    </tr>
    <tr>
        <td>URI:</td>
        <td><?= $request->getUri() ?></td>
    </tr>
    <tr>
        <td>query:</td>
        <td>
            <pre><?= dump($request->getQueryParams()) ?></pre>
        </td>
    </tr>
    <tr>
        <td>post:</td>
        <td>
            <pre><?= dump($request->getParsedBody()) ?></pre>
        </td>
    </tr>
    <tr>
        <td>files:</td>
        <td>
            <pre><?= dump($request->getUploadedFiles()) ?></pre>
        </td>
    </tr>
    <tr>
        <td>headers:</td>
        <td>
            <pre><?= dump($request->getHeaders()) ?></pre>
        </td>
    </tr>
    <tr>
        <td>Is SSL?</td>
        <td><?= $request->isSSL() ? 'yes' : 'no' ?></td>
    </tr>
    <tr>
        <td>Is DNT?</td>
        <td><?= $request->isDoNotTrack() ? 'yes' : 'no' ?></td>
    </tr>
    <tr>
        <td>Is Javascript?</td>
        <td><?= $request->isJavascript() ? 'yes' : 'no' ?></td>
    </tr>
</table>

<h1>Server</h1>
<table>
    <tr>
        <td>IP:</td>
        <td><?= $server->getIP() ?></td>
    </tr>
    <tr>
        <td>port:</td>
        <td><?= $server->getPort() ?></td>
    </tr>
</table>

<h1>Client</h1>
<table>
    <tr>
        <td>IP:</td>
        <td><?= $client->getIP() ?></td>
    </tr>
    <tr>
        <td>port:</td>
        <td><?= $client->getPort() ?></td>
    </tr>
    <tr>
        <td>Is headless?</td>
        <td><?= $client->isHeadless() ? 'yes' : 'no' ?></td>
    </tr>
</table>

<h1>User-Agent</h1>
<table>
    <tr>
        <td>browser:</td>
        <td><?= $userAgent->getBrowser() ?></td>
    </tr>
    <tr>
        <td>platform:</td>
        <td><?= $userAgent->getPlatform() ?></td>
    </tr>
    <tr>
        <td>device:</td>
        <td><?= $userAgent->getDeviceType() ?></td>
    </tr>
    <tr>
        <td>Is bot?</td>
        <td><?= $userAgent->isBot() ? 'yes' : 'no' ?></td>
    </tr>
</table>

<h1>Cache-Control</h1>
<table>
    <tr>
        <td>Has max-age?</td>
        <td><?= $cacheControl->hasMaxAge() ? 'yes' : 'no' ?></td>
    </tr>
    <tr>
        <td>max-age:</td>
        <td><?= $cacheControl->getMaxAge() ?></td>
    </tr>
    <tr>
        <td>Is refresh?</td>
        <td><?= $cacheControl->isRefresh() ? 'yes' : 'no' ?></td>
    </tr>
    <tr>
        <td>Is reload?</td>
        <td><?= $cacheControl->isReload() ? 'yes' : 'no' ?></td>
    </tr>
</table>

<h1>Accept</h1>
<ol>
    <?php foreach ($acceptMediaType->getMediaTypes() as $mediaType): ?>
        <li><?= $mediaType ?></li>
    <?php endforeach; ?>
</ol>

<h1>Accept-Encoding</h1>
<ol>
    <?php foreach ($acceptEncoding->getEncodings() as $encoding) : ?>
        <li><?= $encoding ?></li>
    <?php endforeach; ?>
</ol>

<h1>Accept-Language</h1>
<ol>
    <?php foreach ($acceptLanguage->getLanguages() as $language): ?>
        <li><?= $language ?></li>
    <?php endforeach; ?>
</ol>

<h1>Accept-Charset</h1>
<ol>
    <?php foreach ($acceptCharset->getCharsets() as $charset): ?>
        <li><?= $charset ?></li>
    <?php endforeach; ?>
</ol>
</body>
</html>

Testing

$ composer test

License

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

binsoul/net-http-request 适用场景与选型建议

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

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-10-11