Requests for PHP
, (*1)
Requests is a HTTP library written in PHP, for human beings. It is roughly
based on the API from the excellent Requests Python
library. Requests is ISC
Licensed (similar to
the new BSD license) and has no dependencies, except for PHP 5.6+., (*2)
Despite PHP's use as a language for the web, its tools for sending HTTP requests
are severely lacking. cURL has an
interesting API, to say the
least, and you can't always rely on it being available. Sockets provide only low
level access, and require you to build most of the HTTP response parsing
yourself., (*3)
We all have better things to do. That's why Requests was born., (*4)
$headers = array('Accept' => 'application/json');
$options = array('auth' => array('user', 'pass'));
$request = WpOrg\Requests\Requests::get('https://api.github.com/gists', $headers, $options);
var_dump($request->status_code);
// int(200)
var_dump($request->headers['content-type']);
// string(31) "application/json; charset=utf-8"
var_dump($request->body);
// string(26891) "[...]"
Requests allows you to send HEAD, GET, POST, PUT, DELETE,
and PATCH HTTP requests. You can add headers, form data, multipart files,
and parameters with basic arrays, and access the response data in the same way.
Requests uses cURL and fsockopen, depending on what your system has available,
but abstracts all the nasty stuff out of your way, providing a consistent API., (*5)
Features
- International Domains and URLs
- Browser-style SSL Verification
- Basic/Digest Authentication
- Automatic Decompression
- Connection Timeouts
Installation
Install with Composer
If you're using Composer to manage
dependencies, you can add Requests with it., (*6)
composer require rmccue/requests
or, (*7)
{
"require": {
"rmccue/requests": "^2.0"
}
}
Install source from GitHub
To install the source code:, (*8)
$ git clone git://github.com/WordPress/Requests.git
Next, include the autoloader in your scripts:, (*9)
require_once '/path/to/Requests/src/Autoload.php';
You'll probably also want to register the autoloader:, (*10)
WpOrg\Requests\Autoload::register();
Install source from zip/tarball
Alternatively, you can fetch a tarball or zipball:, (*11)
$ curl -L https://github.com/WordPress/Requests/tarball/stable | tar xzv
(or)
$ wget https://github.com/WordPress/Requests/tarball/stable -O - | tar xzv
Using a Class Loader
If you're using a class loader (e.g., Symfony Class Loader) for
PSR-4-style class loading:, (*12)
$loader = new Psr4ClassLoader();
$loader->addPrefix('WpOrg\\Requests\\', 'path/to/vendor/Requests/src');
$loader->register();
Documentation
The best place to start is our prose-based documentation, which will guide
you through using Requests., (*13)
After that, take a look at the documentation for
\WpOrg\Requests\Requests::request()
, where all the parameters are fully
documented., (*14)
Requests is 100% documented with PHPDoc.
If you find any problems with it, create a new
issue!, (*15)
Testing
Requests strives to have 100% code-coverage of the library with an extensive
set of tests. We're not quite there yet, but we're getting close., (*16)
To run the test suite, first check that you have the PHP
JSON extension enabled. Then
simply:, (*17)
$ phpunit
If you'd like to run a single set of tests, specify just the name:, (*18)
$ phpunit Transport/cURL
Requests and PSR-7/PSR-18
PSR-7 describes common interfaces for representing HTTP messages.
PSR-18 describes a common interface for sending HTTP requests and receiving HTTP responses., (*19)
Both PSR-7 as well as PSR-18 were created after Requests' conception.
At this time, there is no intention to add a native PSR-7/PSR-18 implementation to the Requests library., (*20)
However, the amazing Artur Weigandt has created a package, which allows you to use Requests as a PSR-7 compatible PSR-18 HTTP Client.
If you are interested in a PSR-7/PSR-18 compatible version of Requests, we highly recommend you check out this package., (*21)
Contribute
- Check for open issues or open a new issue for a feature request or a bug.
- Fork the repository on Github to start making your changes to the
develop
branch (or branch off of it).
- Write one or more tests which show that the bug was fixed or that the feature works as expected.
- Send in a pull request.
If you have questions while working on your contribution and you use Slack, there is
a [#core-http-api] channel available in the [WordPress Slack] in which contributions can be discussed., (*22)