dev-master
9999999-dev https://github.com/thomseddon/httpBeautifully simple PHP HTTP client inspired by node's superagent
MIT
The Requires
- php >=5.3
- ext-curl *
- ext-json *
The Development Requires
by Thom Seddon
curl http
Beautifully simple PHP HTTP client inspired by node's superagent
Beautifully simple PHP HTTP client inspired by node's superagent., (*1)
Consider this a beta release, (*2)
Download manually or via composer:, (*3)
composer.phar require thomseddon/http:dev-master
Then require, (*4)
# 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:, (*5)
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, (*6)
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
, (*7)
Return an HTTP_Request instance with method set to POST
, (*8)
Return an HTTP_Request instance with method set to PUT
, (*9)
Return an HTTP_Request instance with method set to DELETE
, (*10)
Two keys can be set:, (*11)
base
string - This string will be prepended to all url
's during send()
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, (*12)
Emit an event, $data
will be passed to any handlers as the first argument, (*13)
All methods (except send) return the $this
and so can be chained for extra fun., (*14)
Set HTTP method, (*15)
Set URL, (*16)
Set headers, the following are equivalent:, (*17)
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, (*18)
Set's json body: json_encodes
data and sets Content-Type
and Content-Length
headers, (*19)
Attach a file, the following are equivalent:, (*20)
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
, (*21)
bool
indicated if the request was not successful, (*22)
HTTP status code, (*23)
Response body, (*24)
phpunit
Thom Seddon, (*25)
The MIT License, (*26)
Copyright (c) 2014 Thom Seddon, (*27)
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:, (*28)
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software., (*29)
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., (*30)
Beautifully simple PHP HTTP client inspired by node's superagent
MIT
curl http