Skip to content
HTTP request library
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
src
tests
.gitignore
.scrutinizer.yml
.travis.yml
CHANGELOG.md
CONTRIBUTING.md
LICENSE
README.md
composer.json
phpunit.xml.dist

README.md

comodojo/httprequest

Build Status Latest Stable Version Total Downloads Latest Unstable Version License Scrutinizer Code Quality Code Coverage

HTTP request library

This is the development branch, please do not use it in production

Main features:

  • 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:

composer require comodojo/httprequest

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).

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.

  • Using get:

    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:

    $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)

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

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

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

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

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

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

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

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

    Force the stream to ignore errors and to return http code and content from the server instead of throwing an exception.

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

Class getters

  • Get response headers:

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

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

Multiple requests

The reset method helps resetting options and data channel; for example:

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.

License

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

You can’t perform that action at this time.