2017 © Pedro Peláez
 

library httprequest

HTTP request library

image

comodojo/httprequest

HTTP request library

  • Tuesday, January 2, 2018
  • by comodojo
  • Repository
  • 3 Watchers
  • 0 Stars
  • 3,372 Installations
  • PHP
  • 3 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 6 Versions
  • 17 % Grown

The README.md

comodojo/httprequest

Build Status Latest Stable Version Total Downloads Latest Unstable Version License Scrutinizer Code Quality Code Coverage, (*1)

HTTP request library, (*2)

This is the development branch, please do not use it in production, (*3)

Main features:, (*4)

  • BASIC, NTLM, DIGEST and SPNEGO auth (requires php curl library) authentication support
  • proxy support
  • allowed http methods: GET, POST, PUT, DELETE
  • CURL or stream working mode
  • request customization (useragent, http version, content type, ...)

Installation

Install composer, then:, (*5)

composer require comodojo/httprequest, (*6)

Basic usage

Library usage is trivial: first create an instance of Httprequest specifing remote host address, then use get or send method to start request. It's important to wrap code in a try/catch block to handle exceptions (if any)., (*7)

Constructor accepts two parameters: remote host address (required) and a boolean value (optional) that, if false, will force lib to use streams instead of curl., (*8)

  • Using get:, (*9)

    try {
    
        // create an instance of Httprequest
        $http = new \Comodojo\Httprequest\Httprequest("www.example.com");
    
        // or:
        // $http = new \Comodojo\Httprequest\Httprequest();
        // $http->setHost("www.example.com");
    
        // get remote data
        $result = $http->get();
    
    } catch (\Comodojo\Exception\HttpException $he) {
    
        /* handle http specific exception */
    
    } catch (\Exception $e) {
    
        /* handle generic exception */
    
    }
    
    
  • Using send:, (*10)

    $data = array('foo'=>'bar', 'baz'=>'boom');
    
    try {
    
        // create an instance of Httprequest
        $http = new \Comodojo\Httprequest\Httprequest("www.example.com");
    
        // get remote data
        $result = $http->setHttpMethod("POST")->send($data);
    
    } catch (\Comodojo\Exception\HttpException $he) {
    
        /* handle http specific exception */
    
    } catch (\Exception $e) {
    
        /* handle generic exception */
    
    }
    
    

Class setters (chainable methods)

  • Set destination port (default 80), (*11)

    $http->setPort(8080);
    
    
  • Set timeout (in secs), (*12)

    $http->setTimeout(10);
    
    
  • Set a custom user agent (default to 'Comodojo-Httprequest'), (*13)

    $http->setUserAgent("My-Custom-User-Agent");
    
    
  • Set HTTP version (1.0 or 1.1), (*14)

    $http->setHttpVersion("1.1");
    
    
  • Set content type (default to 'application/x-www-form-urlencoded' and used only with send method), (*15)

    $http->setContentType("multipart/form-data");
    
    
  • Set additional/custom headers:, (*16)

    $http->setHeader("My-Header","foo");
    
    
  • Set authentication:, (*17)

    // NTLM
    $http->setAuth("NTLM", "myusername", "mypassword");
    
    // BASIC
    $http->setAuth("BASIC", "myusername", "mypassword");
    
    
  • Set proxy:, (*18)

    // No authentication
    $http->setProxy(proxy.example.org);
    
    // Authentication
    $http->setProxy(proxy.example.org, "myusername", "mypassword");
    
    
  • Ignore errors (stream only):, (*19)

    Force the stream to ignore errors and to return http code and content from the server instead of throwing an exception., (*20)

    // Set the stream to ignore errors
    $http->setIgnoreErrors(true);
    
    

Class getters

  • Get response headers:, (*21)

    // After a request...
    
    $headers = $http->getReceivedHeaders();
    
    
  • Get HTTP received status code:, (*22)

    // After a request...
    
    $code = $http->getHttpStatusCode();
    
    

Multiple requests

The reset method helps resetting options and data channel; for example:, (*23)

try {

    // create an instance of Httprequest
    $http = new \Comodojo\Httprequest\Httprequest();

    // first request
    $first_data = $http->setHost("www.example.com")->get();

    // channel reset
    $http->reset();

    // second request
    $second_data = $http->setHost("www.example2.com")->setHttpMethod("POST")->send(array("my"=>"data"));

} catch (\Comodojo\Exception\HttpException $he) {

    /* handle http specific exception */

} catch (\Exception $e) {

    /* handle generic exception */

}

Documentation

Contributing

Contributions are welcome and will be fully credited. Please see CONTRIBUTING for details., (*24)

License

comodojo/httprequest is released under the MIT License (MIT). Please see License File for more information., (*25)

The Versions

02/01 2018

1.2.x-dev

1.2.9999999.9999999-dev https://comodojo.org

HTTP request library

  Sources   Download

MIT

The Requires

 

The Development Requires

http request proxy basic comodojo ntlm

02/01 2018

dev-master

9999999-dev https://comodojo.org

HTTP request library

  Sources   Download

MIT

The Requires

 

The Development Requires

http request proxy basic comodojo ntlm

10/10 2015

1.2.1

1.2.1.0 https://comodojo.org

HTTP request library

  Sources   Download

MIT

The Requires

 

The Development Requires

http request proxy basic comodojo ntlm

10/05 2015

1.2.0

1.2.0.0 http://comodojo.org

HTTP request library

  Sources   Download

MIT

The Requires

 

The Development Requires

http request proxy basic comodojo ntlm

13/01 2015

1.1.0

1.1.0.0 http://comodojo.org

HTTP request library for comodojo

  Sources   Download

GPL-3.0+

http request proxy basic comodojo ntlm

22/07 2014

1.0.0

1.0.0.0 http://www.comodojo.org

HTTP request library for comodojo

  Sources   Download

GPL-3.0+

http request comodojo