Home » Php » PHP REST Clients

PHP REST Clients

Posted by: admin April 23, 2020 Leave a comment

Questions:

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?

How to&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.

Answer:

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

Answer:

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

Answer:

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

Answer:

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:

Answer:

I’ve had good success with Zend Rest Client

Answer:

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;
 });

Answer:

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