35

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?

Jamie Rumbelow
  • 4,967
  • 2
  • 30
  • 42

8 Answers8

39

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.

Matt Zukowski
  • 4,469
  • 4
  • 37
  • 38
38

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

Bastian Voigt
  • 5,311
  • 6
  • 47
  • 65
Michael Dowling
  • 5,098
  • 4
  • 32
  • 28
  • 2
    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! – Bastian Voigt Jan 10 '14 at 11:02
13

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

ceejayoz
  • 176,543
  • 40
  • 303
  • 368
8

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);
?>
5

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:

shurikk
  • 534
  • 7
  • 8
2

I've had good success with Zend Rest Client

John Sheehan
  • 77,456
  • 30
  • 160
  • 194
catsby
  • 11,276
  • 3
  • 37
  • 37
  • 3
    Zend_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
  • 3
    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 Aug 02 '11 at 19:21
  • 5
    Haha, the PHP fractal of bad design strikes again. :-) – Prof. Falken Jul 04 '12 at 09:35
1

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;
 });
Evertonvps
  • 29
  • 2
1

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/).

David Weinraub
  • 14,144
  • 4
  • 42
  • 64