comodojo/httprequest
, (*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)