stadly/http 问题修复 & 功能扩展

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

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

stadly/http

最新稳定版本:v1.2.0

Composer 安装命令:

composer require stadly/http

包简介

A PHP library for handling HTTP headers.

README 文档

README

Latest Version on Packagist Software License Coverage Status Quality Score Total Downloads

A PHP library for parsing and building HTTP headers.

Install

Via Composer

$ composer require stadly/http

Usage

Parsing HTTP headers

Header values can be parsed using fromValue on each header class:

use Stadly\Http\Header\Common\ContentType;
use Stadly\Http\Header\Request\IfNoneMatch;
use Stadly\Http\Header\Response\ContentDisposition;

$contentType = ContentType::fromValue($_SERVER['HTTP_CONTENT_TYPE']);
$ifNoneMatch = IfNoneMatch::fromValue($_SERVER['HTTP_IF_NONE_MATCH']);
$contentDisposition = ContentDisposition::fromValue($_SERVER['HTTP_CONTENT_DISPOSITION']);

Header strings can be parsed using HeaderFactory::fromString:

use Stadly\Http\Header\Request\HeaderFactory as RequestHeaderFactory;
use Stadly\Http\Header\Response\HeaderFactory as ResponseHeaderFactory;

$requestHeaders = [
    'Content-Type: text/html; charset=UTF-8',
    'If-Match: "67ab43", "54ed21", W/"7892dd"',
    'Range: bytes=10-100, 200-300',
];
foreach ($requestHeaders as $headerString) {
    $header = RequestHeaderFactory::fromString($headerString);
}

$responseHeaders = [
    'Content-Type: multipart/form-data; boundary="abc def"',
    'Cache-Control: no-cache="foo, bar", max-age=120, must-revalidate',
    "Content-Disposition: attachment; filename=unicorn.jpg; filename*=UTF-8''%F0%9F%A6%84.jpg",
];
foreach ($responseHeaders as $headerString) {
    $header = ResponseHeaderFactory::fromString($headerString);
}

Note that header strings include the header name, while header values do not. The following results in identical headers:

use Stadly\Http\Header\Response\ContentDisposition;
use Stadly\Http\Header\Response\HeaderFactory;

$header1 = ContentDisposition::fromValue('inline; filename=image.jpg');
$header2 = HeaderFactory::fromString('Content-Disposition: inline; filename=image.jpg');

Example usage

Example parsing the If-None-Match request header and using it to determine whether to serve a file. The response headers Content-Disposition and ETag are built for serving the file.

use Stadly\Http\Header\Request\IfNoneMatch;
use Stadly\Http\Header\Response\ContentDisposition;
use Stadly\Http\Header\Response\ETag;
use Stadly\Http\Header\Value\EntityTag\EntityTag;

$entityTag = new EntityTag(md5($filename));
$ifNoneMatch = IfNoneMatch::fromValue($_SERVER['HTTP_IF_NONE_MATCH']);

if ($ifNoneMatch->evaluate($entityTag)) {
    // Serve file.
    $contentDisposition = new ContentDisposition('attachment');
    $contentDisposition->setFilename(basename($filename));
    header((string)$contentDisposition);

    $eTag = new ETag($entityTag);
    header((string)$eTag);

    readfile($filename);
} else {
    // 304 Not modified.
    http_response_code(304);
}

Header fields overview

The checked header fields have been implemented.

Common header fields

Representation Metadata

Payload Semantics

Request header fields

Controls

Conditionals

Content Negotiation

Authentication Credentials

Request Context

Response header fields

Control Data

Validator Header Fields

Authentication Challenges

Response Context

Other

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 magnar@myrtveit.com instead of using the issue tracker.

Credits

License

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-08-13

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固