PHP REST Clients

63,244

Solution 1

So as it turns out, Zend_Rest_Client isn't a REST client at all — it does not support the PUT and DELETE methods for example. After trying to kludge it into working with an actual RESTful service I got fed up and wrote a proper REST client for PHP:

http://github.com/educoder/pest

It's still missing a few things but if it gets picked up I'll put some more work into it.

Here's a usage example with the OpenStreetMap REST service:

<?php

/**
 * This PestXML usage example pulls data from the OpenStreetMap API.
 * (see http://wiki.openstreetmap.org/wiki/API_v0.6)
 **/

require_once 'PestXML.php';

$pest = new PestXML('http://api.openstreetmap.org/api/0.6');

// Retrieve map data for the University of Toronto campus
$map = $pest->get('/map?bbox=-79.39997,43.65827,-79.39344,43.66903');

// Print all of the street names in the map
$streets = $map->xpath('//way/tag[@k="name"]');
foreach ($streets as $s) {
  echo $s['v'] . "\n";
}

?>

Currently it uses curl but I may switch it to HTTP_Request or HTTP_Request2 down the line.

Update: Looks like quite a few people have jumped on this. Pest now has support for HTTP authentication and a bunch of other features thanks to contributors on GitHub.

Solution 2

I wrote a PHP HTTP client called Guzzle. Guzzle is an HTTP client and framework for building REST webservice clients. You can find more information about Guzzle on its website, or go straight to the source on github: https://github.com/guzzle/guzzle

Guzzle provides goodies that most HTTP clients provide (a simpler interface, all of the HTTP methods, and viewing the request/response), but also provides other advanced features:

  • streaming entity bodies
  • exponential backoff
  • a built-in caching forward proxy
  • cookies
  • logging
  • managed persistent connections
  • parallel requests
  • OAuth
  • a plugin architecture that allows you to implement arbitrary authentication schemes
  • Autogenerating a Client API from a JSON service description file

The only drawback: It requires PHP 5.3.3

Solution 3

I tend to just use PHP's built-in cURL support. The CURLOPT_CUSTOMREQUEST option allows you to do PUT/DELETE etc.

Solution 4

I wasn't able to find elegant solution for a long time, didn't like cURL implementations, came up with my own. It supports HTTP authentication, redirects, PUT, etc. because it relies on pecl http module.

Implementation is nice and simple, easy to extend.

More information can be found here:

Solution 5

I've had good success with Zend Rest Client

Share:
63,244
Jamie Rumbelow
Author by

Jamie Rumbelow

Philosophy, Politics and Economics undergraduate at the New College of the Humanities. Freelance PHP, Ruby and Javascripter.

Updated on January 11, 2020

Comments

  • Jamie Rumbelow
    Jamie Rumbelow over 4 years

    I'm trying to connect to a RESTful web service, but I'm having some troubles, especially when sending data over PUT and DELETE. With cURL, PUT requires a file to send, and DELETE is just weird. I'm perfectly capable of writing a client using PHP's socket support and writing the HTTP headers myself, but I wanted to know whether you guys have ever used or seen a REST client for PHP?

  • Matt Zukowski
    Matt Zukowski over 13 years
    Zend_Rest_Client isn't really a REST client at all... more like RPC over HTTP or something.
  • Jamie Rumbelow
    Jamie Rumbelow about 13 years
    Ahh, very nice Matt. Thanks for sharing that.
  • rtacconi
    rtacconi over 12 years
    I think Zend has the wrong idea of what REST is. I confirm Matt's idea. I am trying to use Matt library but the REST server is poorly implemented (civicrm 3.3) and it does not return proper HTTP error codes.
  • rtacconi
    rtacconi over 12 years
    It is too low level and you are messing your code
  • ceejayoz
    ceejayoz over 12 years
    It's not too low level and if you're messing your code you're doing it wrong.
  • rtacconi
    rtacconi over 12 years
    yes it is too low level. Better something like $lib->post($url, $params);
  • Bjørn Otto Vasbotten
    Bjørn Otto Vasbotten over 12 years
    Matt, thanks a lot. Seems several people are getting interested in PEST atm. :)
  • clockworkgeek
    clockworkgeek almost 12 years
    A nice use of HTTP module, just a shame that it's not shipped with PHP as standard.
  • Prof. Falken
    Prof. Falken almost 12 years
    Haha, the PHP fractal of bad design strikes again. :-)
  • thatidiotguy
    thatidiotguy over 11 years
    Matt, how do you go about stopping curl from always doing POST requests with Content-Type: x-www-formurlencoded?
  • halfer
    halfer over 11 years
    +1, giving this a go now. Still on 5.2, so stuff like Guzzle is out for me atm.
  • Lupuz
    Lupuz about 11 years
    Then you can create your own class that does that low level and use it for high level code
  • Bjørn Otto Vasbotten
    Bjørn Otto Vasbotten almost 11 years
    Really fun to see all the renewed activity on Github lately. :)
  • David Weinraub
    David Weinraub over 10 years
  • Bastian Voigt
    Bastian Voigt over 10 years
    Wow, a lot of client libs to choose from... Guzzle is now part of the Drupal 8 core, so I guess it will be around for a while. Going for this one!
  • Alastair Irvine
    Alastair Irvine about 10 years
    Respect/Rest is a REST "server" (more correctly, a controller), not a client.