Beautifully simple PHP HTTP client inspired by node's superagent.
Consider this a beta release
Download manually or via composer:
composer.phar require thomseddon/http:dev-master
Then require
# Autoload
require_once 'vendor/autoload.php'
# Or manually
require_once '/path/to/vendor/thomseddon/http/lib/HTTP.php'
require_once '/path/to/vendor/thomseddon/http/lib/HTTP.php'
$res = HTTP::get('http://httpbin.org/status/418')->send();
var_dump($res->error());
var_dump($res->status());
var_dump($res->body());
Prints:
bool(false)
int(418)
string(135) "
-=[ teapot ]=-
_...._
.' _ _ `.
| ."` ^ `". _,
\_;`"---"`|//
| ;/
\_ _/
`"""`
"
require_once '/path/to/vendor/thomseddon/http/lib/HTTP.php'
$res = HTTP::post('http://httpbin.org/post')->json(array(
'doing' => 'post'
))->send();
var_dump($res->status());
var_dump($res->body());
Prints
int(200)
class stdClass#3 (8) {
...
public $json =>
class stdClass#8 (1) {
public $doing =>
string(4) "post"
}
...
}
Return an HTTP_Request instance with method set to GET
Return an HTTP_Request instance with method set to POST
Return an HTTP_Request instance with method set to PUT
Return an HTTP_Request instance with method set to DELETE
Two keys can be set:
base
string - This string will be prepended to allurl
's duringsend()
headers
array - Array of headers that will be sent with all subsequent requests
HTTP::configure(array(
'base' => 'http://api.mysite.com/v1',
'headers' => array(
'User-Agent' => 'Frontend Service (0.1)'
)
));
HTTP::get('/status'); // GET's http://api.mysite.com/v1/status
Add an handler that will be called when $event
occurs
Emit an event, $data
will be passed to any handlers as the first argument
All methods (except send) return the $this
and so can be chained for extra fun.
Set HTTP method
Set URL
Set headers, the following are equivalent:
HTTP::get('http://google.com')->set(array(
'User-Agent' => 'My Service (0.1)'
));
HTTP::get('http://google.com')->set('User-Agent', 'My Service (0.1)');
HTTP::get('http://google.com')->set('User-Agent: My Service (0.1)');
Set query params, expects key => value
array
Set's json body: json_encodes
data and sets Content-Type
and Content-Length
headers
Attach a file, the following are equivalent:
HTTP::post('http://uploads.com')->attach(array(
'cat' => '/cats/large.png'
));
HTTP::post('http://uploads.com')->attach('cat', '/cats/large.png');
Sends the request, returns a HTTP_Response
bool
indicated if the request was not successful
HTTP status code
Response body
phpunit
The MIT License
Copyright (c) 2014 Thom Seddon
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.