shieldon/psr-http 问题修复 & 功能扩展

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

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

shieldon/psr-http

最新稳定版本:1.2.7

Composer 安装命令:

composer require shieldon/psr-http

包简介

HTTP implementation for PSR standard.

README 文档

README

build codecov Scrutinizer Code Quality License: MIT

This library is a PSR HTTP implementation created for Shieldon firewall 2 , following up the PSR (PHP Standard Recommendation) documents by one hundred percent.

  • PSR-7 (HTTP Message Interfaces)
  • PSR-15 (HTTP Server Request Handlers)
  • PSR-17 (HTTP Factories)

Test Status

Shiledon PSR-HTTP library is strictly tested by unit tests contain almost all conditions that might be happened, if you find any bug or something that can improve this library, please let me know.

Test suite Status
Repository built-in tests Build Status
PSR-7 integration tests Build Status

You can use it on any framework which is compatible with those PSRs.

Install

composer require shieldon/psr-http

Test

composer install
composer test

Quick Start

The simplest way to start implementing PSR-7 on your PHP applications, let's check out the examples below.

Create a server request.

$serverRequest = ServerRequestFactory::fromGlobal();

Create a request.

$request = RequestFactory::fromNew();

Create a server response

$response = ResponseFactory::fromNew();

Create a URI.

// Create a URI contains visitor's information.

/**
 *  Assume a visior is viewing your website page, 
 *  for example, https://yoursite/en/post1.html
 */
$uri = UriFactory::fromGlobal();

echo $uri->getPath();
// Outputs: /en/post1.html

// Create a URI just a new instance.
$uri = UriFactory::fromNew();

echo $uri->getPath();
// Outputs: 

Create a stream instance.

// Create a stream just a new instance.
$stream = StreamFactory::fromNew();

Create an array with UploadedFile structure.

// Let's see the following example, 
// assume we have a superglobal $_FILES looks like this.
$_FILES = [
    'foo' => [
        'name' => 'example1.jpg',
        'type' => 'image/jpeg',
        'tmp_name' => '/tmp/php200A.tmp',
        'error' => 0,
        'size' => 100000,
    ]
];

$uploadFileArr = UploadedFileFactory::fromGlobal();

echo $uploadFileArr['foo']->getClientFilename();
// Outputs: example1.jpg

Table of Contents

If you are looking for combined examples, see unit testing.

The Behavior of Handling Request Body

Shieldon PSR-HTTP is ready for RESTful, the following content explains how PRS-HTTP deals with the request body.

The getParsedBody method will return:

  • (A) An array of the superglobal $_POST if the request request method is POST and the Content-Type is one of the following types:

    • multipart/form-data
    • application/x-www-form-urlencode
  • (B) A JSON object if the request fit to the following conditions.

    • The request Content-Type is application/json
    • The request body is a valid JSON-formatted string.
    • The request method is not GET.
  • (C) An array parsed from HTTP build query:

    • The condition is neither A or B.
    • The request method is not GET.
  • (D) null if the condition is none of above.

Summary

Condition Method Content-type Parsed-body
A POST multipart/form-data
application/x-www-form-urlencode
array
B ALL excepts GET application/json object
C ALL excepts GET All excepts A or B array
D - - null

Hope this helps.

Author

Shieldon PSR HTTP library is brought to you by Terry L. from Taiwan.

License

MIT

References

  • PSR-7 (HTTP Message Interfaces)
  • PSR-15 (HTTP Server Request Handlers)
  • PSR-17 (HTTP Factories)

统计信息

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

GitHub 信息

  • Stars: 18
  • Watchers: 3
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-06-17