14

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.

1
  • An upvote reminded me I never accepted an answer here. I ended up using Pest as linked in my question - it does the job well, without too much fanfare or extra code that I'm not using. I ended up just not implementing exponential backoff, however - if I do need to, I will give Guzzle another look. Commented Jan 7, 2013 at 21:03

3 Answers 3

11

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());
3
  • Good answer, very easy to understand. Commented Sep 5, 2012 at 13:45
  • Where I can download guzzle.phar ?
    – BILL
    Commented Nov 26, 2013 at 10:46
  • Link is broken, but you can find a guzzle.phar file for each release here: github.com/guzzle/guzzle/releases
    – w5m
    Commented Aug 17, 2014 at 18:50
6

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.

-2

cURL is rather verbose, PHAR requires a bunch of setup. If you want a really simple REST option, try this:

print file_get_contents('http://php.net/');

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.