PHPackages                             webberwu/restful-php - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [HTTP &amp; Networking](/categories/http)
4. /
5. webberwu/restful-php

ActiveLibrary[HTTP &amp; Networking](/categories/http)

webberwu/restful-php
====================

This package is forked from rainner/restful-php which parses the raw input body for all RESTful verbs (POST, PUT, DELETE, PATCH, etc.) and provides a better way for working with uploaded files.

1.0.2(6y ago)01.4kMITPHPPHP &gt;=5.4

Since Jan 6Pushed 6y agoCompare

[ Source](https://github.com/webberwu/restful-php)[ Packagist](https://packagist.org/packages/webberwu/restful-php)[ RSS](/packages/webberwu-restful-php/feed)WikiDiscussions master Synced 6d ago

READMEChangelogDependenciesVersions (4)Used By (0)

PHP Restful Package
===================

[](#php-restful-package)

This package is forked from [rainner/restful-php](https://github.com/rainner/restful-php).

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

[](#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](http://php.net/manual/en/reserved.variables.request.php) 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 ''.print_r( $_REQUEST, true ).'' . "\n";
echo ''.print_r( $_FILES, true ).'' . "\n";
exit;
```

**Restful\\Parser** public methods:

NameArgsReturnDescription`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

[](#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:

```

```

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'] ."  \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 " \n";
}
```

**Restful\\Files** public methods:

NameArgsRetuenDescription`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 &amp; Setup

[](#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
```

```
