rainner/restful-php 问题修复 & 功能扩展

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

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

rainner/restful-php

Composer 安装命令:

composer require rainner/restful-php

包简介

Parses the raw input body for all RESTful verbs (POST, PUT, DELETE, PATCH, etc.) and provides a better way for working with uploaded files.

README 文档

README

This is a small package for working with HTTP input data in PHP. When a POST request comes into PHP, the Content-Type and Content-Body (Raw input) of the request are handled and parsed into the _POST/_FILES arrays. This package parses the raw input body for all RESTful verbs (POST, PUT, DELETE, PATCH, etc.) into the _REQUEST/_FILES arrays and provides a better way for working with uploaded files.

HTTP Input Parser Usage

The Parser class will do the same thing PHP already does for GET/POST data, but for all other REQUEST_METHOD verbs and put the data into the $_REQUEST and $_FILES super globals (Without $_COOKIE).

This class looks at the Content-Type of the request to decide how it will parse the raw content body of the request. As long as the request is formatted properly it should work without any issues. Currently it is capable of parsing the following content types:

  • text/plain as INI data.
  • text/html as HTML formatted data.
  • application/json as JSON encoded data.
  • application/x-www-form-urlencoded as URL encoded data.
  • application/xml as XML formatted data.
  • multipart/form-data as Multipart form data.

Multi-dimentional array type property names (multi[level][name]) are also supported and parsed into regular array by this class.

// parse incoming request data
$request = new Restful\Parser();
$request->parse();

// preview
echo '<pre>'.print_r( $_REQUEST, true ).'</pre>' . "\n";
echo '<pre>'.print_r( $_FILES, true ).'</pre>' . "\n";
exit;

Restful\Parser public methods:

Name Args Return Description
resolveMethod() none null Resolve request method verb
resolveInput() none null Resolve content-type and other input data
setContentType() $ctype null Set the content-type to use
setBoundary() $boundary null Set the form-data boundary string to use
setRawInput() $input null Set the raw input string to parse
parse() none boolean Parse the input data for different content types

HTTP Files Manager

The Files class will take the $_FILES array and re-format the structure to make it much easier for working with multiple file uploads and nested multi-dimentional array keys, for example:

<form>
    <input type="file" name="files[avatar]" accept="image/*" />
    <input type="file" name="files[photos][]" accept="image/*" multiple />
</form>

The form above would have two file dialogue buttons, one accepting a single file, and another accepting multiple files. Here is how we would access these files using the Files class:

// import and format files from the $_FILES array
$files = new Restful\Files();
$files->parse();

// work on the avatar image
$files->loopFiles( 'files.avatar', function( $file )
{
    // save to db, process, etc...
    echo $file['tmp_name'] ." <br /> \n";
});

// move photos to a folder
$photos = $files->moveFiles( 'files.photos', '/path/to/uploads' );
foreach( $photos as $photo )
{
    // check each file for upload/move errors
    echo !empty( $photo['error'] ) ? $photo['error'] : 'Success';
    echo "<br /> \n";
}

Restful\Files public methods:

Name Args Retuen Description
blacklist() $ext1, $ext2, ... null Set a list of file extensions to skip when moving
parse() none null Parses a given FILES array
getParsed() none null Returns the new parsed files array structure
getFiles() $key array Returns a list of files for passed string key
moveFiles() $key, $dirname, $organize array Move list of files to a folder for given key
loopFiles() $key, $callback null Loops over list of files and calls a function on each

Installation & Setup

Manual: Clone this repo somewhere in your project and use the included autoloader to load the included classes:

$ cd /path/to/project/libs
$ git clone https://github.com/rainner/restful-php.git
<?php
require( './libs/restful-php/autoloader.php' )

Composer: Run the following composer commands to include this package and install the dependency for your project:

$ composer require rainner/restful-php 1.*

Author

Rainner Lins: @raintek_

License

Licensed under MIT.

rainner/restful-php 适用场景与选型建议

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

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

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

围绕 rainner/restful-php 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

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