Make making requests with PHP suck less.
Req has two components, a PHP class usable on it's own to make HTTP requests, and a command-line binary that is used alongside a JSON document to specify request details, and can also take a second filename argument that will be used as the request body.
Check out the docs for more detailed information and installation instructions.
POST, GET and custom verb requests (through use of the make() method. It's designed to be light-weight, and not as hefty as other HTTP libraries like Guzzle. It supports HTTP-BASIC auth.
The Req PHP class is just a lovely little convenient wrapper for sending requests, but when paired with the command line utility, will allow you template HTTP requests, pipe their output, and feed in contents of files directly from any directory on your computer.
PLEASE NOTE: Namespace support is not available on the 1.0.x version.
Req is available as a PSR-0 composer installable package, and will work with any PHP 5.3+ environment with php-curl installed. Req is continously tested on Travis-CI.
Just add this to your composer.json:
{
"require": {
"req/req": "1.1.*"
}
}Send a GET request:
$req = new Req\Req("http://mysite.com");
$response = $req->get();Set headers:
$req = new Req\Req("http://mysite.com");
$headers = array(
'Content-type' => 'text/html',
'X-Custom-Header' => 'Value',
);
$response = $req->headers($headers)->get();Set post data:
$req = new Req\Req('http://mysite.com');
$postData = array('foo' => 'bar', 'woo' => 'sa');
// Will serialize $postData into foo=bar&woo=sa
$req->post($postData);You can also pass a string of data:
$req = Req\Req::create('http://mysite.com')->post('<xml><item><title>Item1</title></item></xml>');Or even a file (which is essentially what the req command line tool does):
$filepath = '/path/to/my/file';
$contents = file_get_contents($filepath);
$req = Req::create('http://mysite.com')->post($contents);Create a requestfile, which is just simple, valid JSON document, that includes all information for the request. These documents must be valid JSON, and can't include comments. The only parameter that is required is the url parameter:
{
"url":"http://example.com",
"method":"get/post",
"headers": {
"Content-type" : "application/json",
"Accept" : "application/json",
"X-Custom-Header" : "Custom Value"
},
"data" : {
"foo":"bar",
"bar":"foo"
}
}Then send the templated request with the make command:
./req make my_requestfile.jsonYou can also provide a file as a second argument that will be substituted in as the POST data, this will be POSTed as a string:
# Either:
./req make <url> <filepath>
# Or:
./req make <request filename> <filepath>Simple!
