PHP Classes

PHP httpful Request: Send and process HTTP requests using handler class

Recommend this page to a friend!
  Info   View files Example   View files View files (70)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2020-05-15 (Yesterday) RSS 2.0 feedNot yet rated by the usersTotal: Not yet counted Not yet ranked
Version License PHP version Categories
httpful 1.0MIT/X Consortium ...7HTTP, PHP 7
Description Author

This package can send and process HTTP requests using handler class.

It can send HTTP requests with a wrapper class around the PHP Curl extension.

The package can also provide means to register handler classes that can parse HTTP request responses with data with a given MIME type, as well serialize request data of the same MIME type to be sent as payload of the HTTP requests that are sent.

The package already comes with built-in MIME handler classes for format data types like CSV, form data, HTML, JSON and XML.

  Performance   Level  
Name: Lars Moelleken <contact>
Classes: 22 packages by
Country: Germany Germany
Innovation award
Innovation award
Nominee: 9x

Winner: 1x

 

Details

Build Status Coverage Status Codacy Badge Latest Stable Version Total Downloads License Donate to this project using Paypal Donate to this project using Patreon

? Httpful

Forked some years ago from nategood/httpful + added support for parallel request and implemented many PSR Interfaces: A Chainable, REST Friendly Wrapper for cURL with many "PSR-HTTP" implemented inferfaces.

Features

- Readable HTTP Method Support (GET, PUT, POST, DELETE, HEAD, PATCH and OPTIONS) - Custom Headers - Automatic "Smart" Parsing - Automatic Payload Serialization - Basic Auth - Client Side Certificate Auth (SSL) - Request "Download" - Request "Templates" - Parallel Request (via curl_multi) - PSR-3: Logger Interface - PSR-7: HTTP Message Interface - PSR-17: HTTP Factory Interface - PSR-18: HTTP Client Interface

Examples

<?php

// Make a request to the GitHub API.

$uri = 'https://api.github.com/users/voku';
$response = \Httpful\Client::get($uri, null, \Httpful\Mime::JSON);

echo $response->getBody()->name . ' joined GitHub on ' . date('M jS Y', strtotime($response->getBody()->created_at)) . "\n";

<?php

// Make a request to the GitHub API with a custom
// header of "X-Foo-Header: Just as a demo".

$uri = 'https://api.github.com/users/voku';
$response = \Httpful\Client::get_request($uri)->withAddedHeader('X-Foo-Header', 'Just as a demo')
                                              ->expectsJson()
                                              ->send();

$result = $response->getRawBody();

echo $result['name'] . ' joined GitHub on ' . \date('M jS Y', \strtotime($result['created_at'])) . "\n";

<?php

// BasicAuth example with MultiCurl for async requests.

/ @var \Httpful\Response[] $results */
$results = [];
$multi = new \Httpful\ClientMulti(
    static function (\Httpful\Response $response, \Httpful\Request $request) use (&$results) {
        $results[] = $response;
    }
);

$request = (new \Httpful\Request(\Httpful\Http::GET))
    ->withUriFromString('https://postman-echo.com/basic-auth')
    ->withBasicAuth('postman', 'password');

$multi->add_request($request);
// $multi->add_request(...); // add more calls here

$multi->start();

// DEBUG
//print_r($results);

Installation

composer require voku/httpful

Handlers

We can override the default parser configuration options be registering a parser with different configuration options for a particular mime type

Example: setting a namespace for the XMLHandler parser

$conf = ['namespace' => 'http://example.com'];
\Httpful\Setup::registerMimeHandler(\Httpful\Mime::XML, new \Httpful\Handlers\XmlMimeHandler($conf));

Handlers are simple classes that are used to parse response bodies and serialize request payloads. All Handlers must implement the MimeHandlerInterface interface and implement two methods: serialize($payload) and parse($response). Let's build a very basic Handler to register for the text/csv mime type.

<?php

class SimpleCsvMimeHandler extends \Httpful\Handlers\DefaultMimeHandler
{
    /
     * Takes a response body, and turns it into
     * a two dimensional array.
     *
     * @param string $body
     *
     * @return array
     */
    public function parse($body)
    {
        return \str_getcsv($body);
    }

    /
     * Takes a two dimensional array and turns it
     * into a serialized string to include as the
     * body of a request
     *
     * @param mixed $payload
     *
     * @return string
     */
    public function serialize($payload)
    {
        // init
        $serialized = '';

        foreach ($payload as $line) {
            $serialized .= '"' . \implode('","', $line) . '"' . "\n";
        }

        return $serialized;
    }
}

\Httpful\Setup::registerMimeHandler(\Httpful\Mime::CSV, new SimpleCsvMimeHandler());

Finally, you must register this handler for a particular mime type.

\Httpful\Setup::register(Mime::CSV, new SimpleCsvHandler());

After this registering the handler in your source code, by default, any responses with a mime type of text/csv should be parsed by this handler.

  Files folder image Files  
File Role Description
Files folder imageexamples (5 files)
Files folder imagesrc (1 directory)
Files folder imagetests (1 file, 2 directories)
Accessible without login Plain text file .editorconfig Data Auxiliary data
Accessible without login Plain text file .scrutinizer.yml Data Auxiliary data
Accessible without login Plain text file .styleci.yml Data Auxiliary data
Accessible without login Plain text file .travis.yml Data Auxiliary data
Accessible without login Plain text file build.sh Data Auxiliary data
Accessible without login Plain text file CHANGELOG.md Data Auxiliary data
Accessible without login Plain text file circle.yml Data Auxiliary data
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file LICENSE.txt Doc. Documentation
Accessible without login Plain text file phpcs.php_cs Example Example script
Accessible without login Plain text file phpstan.neon Data Auxiliary data
Accessible without login Plain text file phpunit.xml.dist Data Auxiliary data
Accessible without login Plain text file README.md Doc. Read m

  Files folder image Files  /  examples  
File Role Description
  Accessible without login Plain text file github.php Example Example script
  Plain text file override.php Class Class source
  Accessible without login Plain text file scraping_imdb.php Example Example script
  Accessible without login Plain text file scraping_multi.php Example Example script
  Accessible without login Plain text file xml.php Example Example script

  Files folder image Files  /  src  
File Role Description
Files folder imageHttpful (17 files, 3 directories)

  Files folder image Files  /  src  /  Httpful  
File Role Description
Files folder imageCurl (3 files)
Files folder imageException (8 files)
Files folder imageHandlers (8 files)
  Plain text file Client.php Class Class source
  Plain text file ClientMulti.php Class Class source
  Plain text file ClientPromise.php Class Class source
  Plain text file Encoding.php Class Class source
  Plain text file Factory.php Class Class source
  Plain text file Headers.php Class Class source
  Plain text file Http.php Class Class source
  Plain text file Mime.php Class Class source
  Plain text file Proxy.php Class Class source
  Plain text file Request.php Class Class source
  Plain text file Response.php Class Class source
  Plain text file ServerRequest.php Class Class source
  Plain text file Setup.php Class Class source
  Plain text file Stream.php Class Class source
  Plain text file UploadedFile.php Class Class source
  Plain text file Uri.php Class Class source
  Plain text file UriResolver.php Class Class source

  Files folder image Files  /  src  /  Httpful  /  Curl  
File Role Description
  Plain text file Curl.php Class Class source
  Plain text file MultiCurl.php Class Class source
  Plain text file MultiCurlPromise.php Class Class source

  Files folder image Files  /  src  /  Httpful  /  Exception  
File Role Description
  Plain text file ClientErrorException.php Class Class source
  Plain text file CsvParseException.php Class Class source
  Plain text file JsonParseException.php Class Class source
  Plain text file NetworkErrorException.php Class Class source
  Plain text file RequestException.php Class Class source
  Plain text file ResponseException.php Class Class source
  Plain text file ResponseHeaderException.php Class Class source
  Plain text file XmlParseException.php Class Class source

  Files folder image Files  /  src  /  Httpful  /  Handlers  
File Role Description
  Plain text file AbstractMimeHandler.php Class Class source
  Plain text file CsvMimeHandler.php Class Class source
  Plain text file DefaultMimeHandler.php Class Class source
  Plain text file FormMimeHandler.php Class Class source
  Plain text file HtmlMimeHandler.php Class Class source
  Plain text file JsonMimeHandler.php Class Class source
  Plain text file MimeHandlerInterface.php Class Class source
  Plain text file XmlMimeHandler.php Class Class source

  Files folder image Files  /  tests  
File Role Description
Files folder imageHttpful (12 files)
Files folder imagestatic (3 files)
  Accessible without login Plain text file bootstrap.php Aux. Auxiliary script

  Files folder image Files  /  tests  /  Httpful  
File Role Description
  Plain text file ClientMultiTest.php Class Class source
  Plain text file ClientPromiseTest.php Class Class source
  Plain text file ClientTest.php Class Class source
  Plain text file DevtoTest.php Class Class source
  Plain text file FactoryTest.php Class Class source
  Plain text file HttpfulTest.php Class Class source
  Plain text file RequestTest.php Class Class source
  Plain text file ResponseTest.php Class Class source
  Plain text file ServerRequestTest.php Class Class source
  Plain text file StreamTest.php Class Class source
  Plain text file UploadedFileTest.php Class Class source
  Plain text file UriTest.php Class Class source

  Files folder image Files  /  tests  /  static  
File Role Description
  Accessible without login Plain text file foo.txt Doc. Documentation
  Accessible without login Plain text file test.json Data Auxiliary data
  Accessible without login Image file test_image.jpg Icon Icon image

Downloadhttpful-2020-05-15.zip
Downloadhttpful-2020-05-15.tar.gz
Install with ComposerInstall with Composer
Needed packages  
Class DownloadWhy it is needed Dependency
Simple HTML DOM Download .zip .tar.gz auto-parsing html output Required
 Version Control Unique User Downloads  
 100%
Total:0
This week:0

For more information send a message to info at phpclasses dot org.