2017 © Pedro Peláez
 

library http

Beautifully simple PHP HTTP client inspired by node's superagent

image

thomseddon/http

Beautifully simple PHP HTTP client inspired by node's superagent

  • Tuesday, July 8, 2014
  • by thomseddon
  • Repository
  • 1 Watchers
  • 3 Stars
  • 788 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 1 Versions
  • 8 % Grown

The README.md

HTTP Build Status

Beautifully simple PHP HTTP client inspired by node's superagent., (*1)

Consider this a beta release, (*2)

Installation

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'

Examples

GET


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 ]=-

       _...._
     .'  _ _ `.
    | ."` ^ `". _,
    \_;`"---"`|//
      |       ;/
      \_     _/
        `"""`
"

POST JSON

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" } ... }

API

HTTP

HTTP::get(string $url)

Return an HTTP_Request instance with method set to GET, (*7)

HTTP::post(string $url)

Return an HTTP_Request instance with method set to POST, (*8)

HTTP::put(string $url)

Return an HTTP_Request instance with method set to PUT, (*9)

HTTP::del(string $url)

Return an HTTP_Request instance with method set to DELETE, (*10)

HTTP::configure(array $config)

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

HTTP::on(string $event, function $handler)

Add an handler that will be called when $event occurs, (*12)

HTTP::emit(string $event, mixed $data = null)

Emit an event, $data will be passed to any handlers as the first argument, (*13)

HTTP_Request

All methods (except send) return the $this and so can be chained for extra fun., (*14)

method(string $method)

Set HTTP method, (*15)

url(string $url)

Set URL, (*16)

set(mixed $data, mixed $value = false)

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)');

query(array $query)

Set query params, expects key => value array, (*18)

json(mixed $data)

Set's json body: json_encodes data and sets Content-Type and Content-Length headers, (*19)

attach(mixed $files, $path = null)

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');

send()

Sends the request, returns a HTTP_Response, (*21)

HTTP Response

error()

bool indicated if the request was not successful, (*22)

status()

HTTP status code, (*23)

body()

Response body, (*24)

Testing

phpunit

Author

Thom Seddon, (*25)

License

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)

The Versions

08/07 2014

dev-master

9999999-dev https://github.com/thomseddon/http

Beautifully simple PHP HTTP client inspired by node's superagent

  Sources   Download

MIT

The Requires

  • php >=5.3
  • ext-curl *
  • ext-json *

 

The Development Requires

curl http