2017 © Pedro Peláez
 

library http

Improved HTTP for PHP

image

sndsgd/http

Improved HTTP for PHP

  • Saturday, December 16, 2017
  • by sndsgd
  • Repository
  • 1 Watchers
  • 1 Stars
  • 925 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 2 Forks
  • 1 Open issues
  • 14 Versions
  • 0 % Grown

The README.md

sndsgd/http

Latest Version Software License Build Status Coverage Status Total Downloads, (*1)

Improved HTTP for PHP., (*2)

Requirements

You need PHP >= 7.1 to use this library, however, the latest stable version of PHP is recommended., (*3)

Install

Install sndsgd/http using Composer., (*4)

Usage

This library aims to improve how PHP handles HTTP requests, mainly by streamlining the the process of working with request parameters., (*5)

Improved Request Parameter Decoders

To experiment with the decoders, you can use PHP's built in webserver with the script located in bin/request-demo.php., (*6)

php -S localhost:8000 bin/request-demo.php

Now you will be able to make requests to http://localhost:8000 using any HTTP client. In the examples below, we'll be using httpie. To dump information about the request that is being made, simply append -v to any of the commands below., (*7)

Query String Decoder

In many query parameter implementations, you do not need to use brackets to indicate multiple values, but with PHP you do. For compatibility with the built in PHP decoder, you can continue to use brackets., (*8)

http http://localhost:8000/query a==1 a==2 a[]==💩

Result:, (*9)

    "$_GET": {
        "a": [
            "💩"
        ]
    },
    "sndsgd": {
        "a": [
            "1",
            "2",
            "💩"
        ]
    }
}

Request Body Decoder

The built in body decoder for PHP only handles multipart/form-data and application/x-www-form-urlencoded content types for POST requests. This library acts as a polyfill to add application/json to that list, and to allow for decoding the request body for any request method. By default, POST requests will be processed by the built in PHP decoder, however, you can disable that functionality by setting enable_post_data_reading = Off in your php.ini., (*10)

The built in decoder will not decode the body of this urlencoded PATCH request, (*11)

http --form PATCH http://localhost:8000/body a=1 b=2 c[d]=3a

Result:, (*12)

{
    "$_POST": [],
    "$_FILES": [],
    "sndsgd": {
        "a": "1",
        "b": "2",
        "c": {
            "d": "3"
        }
    }
}

Uploaded Files

Instead of using $_FILES, with sndsgd/http uploaded files are included with the other parameters of the request body as objects., (*13)

http --form POST http://localhost:8000/body a=1 file@README.md

Result:, (*14)

{
    "$_POST": {
        "a": "1"
    },
    "$_FILES": {
        "file": {
            "name": "README.md",
            "type": "",
            "tmp_name": "/private/var/folders/2b/mtmy5wk56jx13vjgqpydc3nr0000gn/T/php9DTXiq",
            "error": 0,
            "size": 2766
        }
    },
    "sndsgd": {
        "a": "1",
        "file": {
            "filename": "README.md",
            "contentType": "text/x-markdown",
            "realContentType": "text/plain",
            "size": 2766,
            "tempfile": "/private/var/folders/2b/mtmy5wk56jx13vjgqpydc3nr0000gn/T/php9DTXiq"
        }
    }
}

The Versions

24/06 2017

dev-uploaded-file-error-toArray

dev-uploaded-file-error-toArray https://github.com/sndsgd/http

Improved HTTP for PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

10/06 2017
12/07 2016

0.9.0

0.9.0.0 https://github.com/sndsgd/http

Improved HTTP for PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

29/05 2015

0.5.1

0.5.1.0 https://github.com/sndsgd/sndsgd-http

A collection of tools for working with HTTP

  Sources   Download

MIT

The Requires

 

The Development Requires

12/01 2015

0.5.0

0.5.0.0 https://github.com/sndsgd/sndsgd-http

A collection of tools for working with HTTP

  Sources   Download

MIT

The Requires

 

The Development Requires

01/01 2015

0.1.1

0.1.1.0 https://github.com/sndsgd/sndsgd-http

A collection of tools for working with HTTP

  Sources   Download

MIT

The Requires

 

The Development Requires

11/11 2014

0.1.0

0.1.0.0 https://github.com/sndsgd/sndsgd-http

A collection of tools for working with HTTP

  Sources   Download

MIT

The Requires

 

The Development Requires