Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple but established/complete REST client for PHP?

Tags:

rest

http

php

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:

  • HTTP Basic Authentication
  • Following redirects
  • Backing off and re-requesting of failed requests

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.

like image 318
cincodenada Avatar asked Feb 16 '12 02:02

cincodenada


2 Answers

There are a lot of advanced features in Guzzle, but I still think you can easily use it for your minimal requirements.

  1. Download the phar file that includes all of the classes you'll need from their GitHub release page: https://github.com/guzzle/guzzle/releases
  2. Include the phar file in your application (this will configure an autoloader for Guzzle)
  3. Create a client
  4. Attach an ExponentialBackoffPlugin to the client
  5. Send requests from the client

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());
like image 146
Michael Dowling Avatar answered Sep 17 '22 14:09

Michael Dowling


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.

like image 38
Francis Avila Avatar answered Sep 20 '22 14:09

Francis Avila