2017 © Pedro Peláez
 

library httpquest

PHP polyfill for HTTP request parsing

image

dejan7/httpquest

PHP polyfill for HTTP request parsing

  • Tuesday, September 20, 2016
  • by dejan7
  • Repository
  • 1 Watchers
  • 4 Stars
  • 96 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 4 Versions
  • 1 % Grown

The README.md

Build Status Coverage Status, (*1)

HTTPQuest

HTTPQuest is a lightweight PHP polyfill that can parse incoming request body for any HTTP verb., (*2)

The problem:

PHP by default parses request data only for GET and POST requests and puts them in well-known $_GET and $_POST and $_FILES variables. Additionally it does so only for multipart/form-data and application/x-www-form-urlencoded enctypes (Content-type HTTP header)., (*3)

So if you are trying to build a REST API in plain PHP (without a framework) which utilizes other HTTP verbs like PUT or PATCH - it turns out to be a pain. This package attempts to solve that by doing the boring parsing for you and making the request data available for any type of request as well., (*4)

How to install?

composer require dejan7/httpquest:0.3.0, (*5)

How to use?

Instantiate the class and call the decode method somewhere near the beginning of your app (e.g. during bootstrapping)., (*6)

$HTTPQuest = new \HTTPQuest\HTTPQuest();
$HTTPQuest->decode($_POST, $_FILES);

And that's all, your parsed data will now be in variables that you passed to the decode method ($_POST, and $_FILES in this case)., (*7)

PHP puts stuff in $_POST and $_FILES by default for following cases:, (*8)

  • POST : Content-types:
    • application/x-www-form-urlencoded
    • multipart/form-data

HTTPQuest enhances this and you can configure it to parse data for any method/content-type combination. The default options are set to this:, (*9)

  • POST: Content-types:
    • application/json
  • PUT: Content-types:
    • application/x-www-form-urlencoded
    • multipart/form-data
    • application/json
  • PATCH: Content-types:
    • application/x-www-form-urlencoded
    • multipart/form-data
    • application/json

However, you can change the defaults by passing a HTTPQuestOptions instance to the constructor like this:, (*10)

include __DIR__ . "/../vendor/autoload.php";

use HTTPQuest\HTTPQuestOptions;
use HTTPQuest\Requests;
use HTTPQuest\ContentTypes;


$opts = new HTTPQuestOptions();

$opts->forMethod(Requests::GET)
    ->parse(ContentTypes::X_WWW_FORM_URLENCODED);

$opts->forMethod(Requests::PATCH)
    ->parse(ContentTypes::FORMDATA)
    ->parse(ContentTypes::JSON);

$HTTPQuest = new \HTTPQuest\HTTPQuest(
    $_SERVER,
    "php://input",
    $opts
);

Wait! Are you telling me that it can parse request body even for GET requests? That's correct. Now whether you will utilize such scenarios - i leave the choice to you. You can read some of the discussions on StackOverflow and decide for yourself http://stackoverflow.com/questions/978061/http-get-with-request-body http://stackoverflow.com/questions/299628/is-an-entity-body-allowed-for-an-http-delete-request, (*11)

Files

HTTPQuest also parses the files from incoming requests and tries it's best to mimic PHP's default behavior with $_FILES. However, the only caveat is that you can't use move_uploaded_file PHP function on requests other than POST. On other requests move_uploaded_file thinks that the file wasn't uploaded with PHP and it doesn't execute. The workaround is to use copy like this:, (*12)

$HTTPQuest = new \HTTPQuest\HTTPQuest();
$HTTPQuest->decode($_POST, $_FILES);

copy($_FILES["myfile"]["tmp_name"], "/some/dir" . $_FILES["myfile"]["name"]);

Credits

Russel for sndsgd/http, HTTPQuest uses his multipart/form-data decoding logic., (*13)

License

This is an open-sourced software licensed under the MIT license., (*14)

The Versions

20/09 2016

dev-master

9999999-dev

PHP polyfill for HTTP request parsing

  Sources   Download

The Requires

  • php >=5.6.0

 

The Development Requires

by Avatar dejan7

rest http request put patch

20/09 2016

0.3.0

0.3.0.0

PHP polyfill for HTTP request parsing

  Sources   Download

The Requires

  • php >=5.6.0

 

The Development Requires

by Avatar dejan7

rest http request put patch

16/09 2016

0.2.0

0.2.0.0

Parses PUT/PATCH data into $_POST

  Sources   Download

The Requires

  • php >=5.3.0

 

The Development Requires

by Avatar dejan7

http request put patch

12/09 2016

v0.1.0

0.1.0.0

Parses PUT/PATCH data into $_POST

  Sources   Download

The Requires

  • php >=5.3.0

 

The Development Requires

by Avatar dejan7

http request put patch