I'm looking to implement a REST client in PHP, and have previously been using a modified version of the CakePHP Webservice Behavior, which is pretty close to what I am looking for, but I'm not building on Cake, so that's off the list.
I'm looking for a REST client that allows me to make basic REST requests and get back the headers/body of the request. Optional, but desired features:
I've read through other SO threads and have found a bunch of unhelpful answers that mainly consist of "just write it yourself, it's not hard" or "use cURL" (i.e. same as the previous one) or "here's a code snippet that'll do it" (certainly not established or robust). This question is the most potentially helpful, but it has the above, plus two self-rolled libraries posted by their authors that are very basic and are missing some of the above features and, while they'd probably do the trick, are hardly as established or maintained as I would like.
That question also links to Guzzle, which has the opposite problem - while robust and well-supported, it appears to be hugely complex, difficult to configure and set up, and way more than I'm looking for.
So: does anyone know of an established REST client for PHP that just does REST requests, is small and focused (I'm thinking one, maybe two PHP files), but is widely used and supported? I'm not looking for a framework or a revolution in HTTP usage - just a library to do basic REST requests. I don't want to reinvent the wheel again, and with the thousands (if not millions) of PHP projects that use REST every day, I'm surprised I haven't found something like this.
There are plenty of quality REST server libraries available - why not REST clients? I'm basically looking for something like Tonic, but as a client. Pest (from above) looks well-built, and is closest, but I'm just surprised there isn't something more established.
There are a lot of advanced features in Guzzle, but I still think you can easily use it for your minimal requirements.
Example:
<?php
// Include the phar and register the autoloader
require 'guzzle.phar';
// Create a client for http://test.com (binding to a host is optional)
$client = new Guzzle\Http\Client('http://test.com');
// Attach the exponential backoff plugin to the client so that requests
// are retried automatically
$client->addSubscriber(Guzzle\Plugin\Backoff\BackoffPlugin::getExponentialBackoff());
// Create and send a GET request to grab the response
$response = $client->get('/foo.json')->send();
// Dump the JSON response data as an array
var_dump($response->json());
You may look at Requests or even the HTTP PECL library. However, these are only concerned with the HTTP request/response process. You may be looking for more than that? The REST design intersects so very much with the HTTP protocol that there isn't much more abstraction that can be built on top of it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With