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?
8 Answers
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.

- 4,469
- 4
- 37
- 38
-
1Ahh, very nice Matt. Thanks for sharing that. – Jamie Rumbelow Feb 09 '11 at 18:02
-
Matt, thanks a lot. Seems several people are getting interested in PEST atm. :) – Bjørn Otto Vasbotten Nov 30 '11 at 12:05
-
1Matt, how do you go about stopping curl from always doing POST requests with Content-Type: x-www-formurlencoded? – thatidiotguy Oct 18 '12 at 13:42
-
+1, giving this a go now. Still on 5.2, so stuff like Guzzle is out for me atm. – halfer Dec 19 '12 at 16:51
-
1Really fun to see all the renewed activity on Github lately. :) – Bjørn Otto Vasbotten Jul 08 '13 at 22:04
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

- 5,311
- 6
- 47
- 65

- 5,098
- 4
- 32
- 28
-
2Wow, 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! – Bastian Voigt Jan 10 '14 at 11:02
I tend to just use PHP's built-in cURL support. The CURLOPT_CUSTOMREQUEST
option allows you to do PUT
/DELETE
etc.

- 176,543
- 40
- 303
- 368
-
8
-
4It's not too low level and if you're messing your code you're doing it wrong. – ceejayoz Aug 02 '11 at 19:29
-
13yes it is too low level. Better something like $lib->post($url, $params); – rtacconi Aug 10 '11 at 15:42
-
3Then you can create your own class that does that low level and use it for high level code – Lupuz Feb 13 '13 at 10:59
simple example in php for the rest client - updating is given below:
<?php
$url ="http://example.com";
$data = "The updated text message";
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT"); //for updating we have to use PUT method.
curl_setopt($ch,CURLOPT_POSTFIELDS,$data);
$result = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
?>
simple example in php for the rest client - deleting of categoryid=xx is given below:
<?php
$url ="http://example.com/categoryid=xx";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
$result = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
?>
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:

- 534
- 7
- 8
-
A nice use of HTTP module, just a shame that it's not shipped with PHP as standard. – clockworkgeek Apr 27 '12 at 21:24
I've had good success with Zend Rest Client

- 77,456
- 30
- 160
- 194

- 11,276
- 3
- 37
- 37
-
3Zend_Rest_Client isn't really a REST client at all... more like RPC over HTTP or something. – Matt Zukowski Jan 08 '11 at 20:04
-
3I 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 Aug 02 '11 at 19:21
-
5
Resurrecting the topic, I found this library https://github.com/Respect/Rest/ is very easy to use, but there are few examples on the web:
require_once 'bootstrap.php';
require_once 'Respect/Rest/Router.php';
require_once 'Respect/Rest/Request.php';
use Respect\Rest\Router;
$router->post('/myApp/', function() {
$data_back = json_decode(file_get_contents('php://input'));
// var_dump($data_back);
return json_encode($data_back);
});
$router->get('/myApp/*', function($id = null) {
$json = json_encode(MyService::getInstance()->list());
return utf8_encode($json);
});
$router->put('/myApp/*', function($id = null) {
return 'Update: ' . $id;
});
$router->delete('/myApp/*', function($id = null) {
return 'Delete: ' . $id;
});

- 29
- 2
-
2Respect/Rest is a REST "server" (more correctly, a controller), not a client. – Alastair Irvine Mar 11 '14 at 10:46
A recent arrival is Zend\Http\Client, part of the Zend Framework 2.
Installable via composer (though, as of this writing, not via Packagist; still need to use Zend's custom package repository http://packages.zendframework.com/).

- 14,144
- 4
- 42
- 64
-
Now on packagist: https://packagist.org/packages/zendframework/zend-http – David Weinraub Dec 19 '13 at 15:38