PHP REST Clients

I'm trying to connect to a RESTful web service, but I'm having some problems, especially when sending data via PUT and DELETE. Using cURL PUT requires sending a file, and DELETE is just weird. I am very good at writing a client using support for PHP sockets and writing HTTP headers myself, but I wanted to know if you ever used guys or saw a REST client for PHP?

+33
rest php client
May 21 '09 at 18:07
source share
8 answers

So, Zend_Rest_Client is not a REST client at all - it does not support PUT and DELETE methods, for example. After trying to clone it with the actual RESTful service, I got tired and wrote the correct REST client for PHP:

http://github.com/educoder/pest

It still lacks a few things, but if it is raised, I will add some more work.

Here is an example of using 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"; } ?> 

It currently uses curl, but I can switch it to HTTP_Request or HTTP_Request2 line by line.

Update: Some people seem to have jumped on this. Pest now supports HTTP authentication and many other features thanks to GitHub contributors.

+38
Jan 20 '11 at 17:26
source share

I wrote a PHP HTTP client called Guzzle. Guzzle is an HTTP client and infrastructure for creating REST web service clients. You can find more information about Guzzle on the website or go directly to the source on github: https://github.com/guzzle/guzzle

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

  • stream entities
  • exponential delay
  • built-in proxy server for caching
  • biscuits
  • entry
  • managed persistent connections
  • parallel queries
  • OAuth
  • plugin architecture that allows arbitrary authentication schemes to be implemented
  • Auto Generate Client API from JSON Service Description File

The only downside: it requires PHP 5.3.3

+33
Oct 12 2018-11-11T00:
source share

I usually use the built-in PHP cURL support . The CURLOPT_CUSTOMREQUEST parameter allows you to do PUT / DELETE , etc.

+12
May 21 '09 at 18:17
source share

simple example in php for the rest of the client - the update 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); ?> 

a simple example in php for the rest of the client is removing the category = xx 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); ?> 
+7
Dec 15 2018-11-11T00:
source share

For a long time I could not find an elegant solution, I did not like the implementation of cURL, I came up with my own. It supports HTTP authentication, forwarding, PUT, etc., because it relies on the pecl http module.

The implementation is simple and simple, easy to expand.

More detailed information can be found here:

+5
Sep 20 '11 at 20:41
source share

I had a good success with Zend Rest Client

+2
May 21 '09 at 19:34
source share

Resuming the topic, I found this library https://github.com/Respect/Rest/ , very easy to use, but there are a few examples on the Internet:

  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; }); 
+1
Jul 31 '12 at 13:08
source share

The recent arrival of Zend \ Http \ Client , part of Zend Framework 2.

It is installed through the composer (although at the time of this writing, and not through Packagist, you still need to use the Zend custom package repository http://packages.zendframework.com/ ).

+1
Nov 28 '12 at 16:09
source share



All Articles