RPC / XML-RPC / JSON-RPC in PHP

2.2k views Asked by At

I am looking up exmples or tutorial of XML-RPC and JSON-RPC in PHP XML-RPC / JSON-RPC client and server

Can someone tell me that?

Thank you? Sorry My english is not good.

2

There are 2 answers

0
sergioska On

I think that best way to implement a json-rpc service is using Zend component Zend_Json_Server.

So i suggest you to use Zend_Json component to implement a json-rpc service in php. Zend framework allows use of its component "out of the box". So you could do a structure like following:

Project
   |
   ------libs/Zend
                |
                -----Json/
                       |
                       -----Server/
                |
                -----Loader.php

And implement something like this:

<?php

  // path to dir with Zend root
  set_include_path(__DIR__ . "/libs");
  // path to Zend loader
  require_once __DIR__ . "/libs/Zend/Loader.php";

  Zend_Loader::loadClass('Zend_Json_Server');

  $server = new Zend_Json_Server();
  $server->setClass('Service');

  /**
   * Service Implementation
   */
  class Service
  {
      public function __construct()
      {
        // init some service attributes ...
      }

      /**
       * example of api method exposed by service
       * return "hello world" message
       * @param $domain
       * @return object (json)
       */
       public function helloworld()
       {
            $aOut = array('msg' => 'hello world');
            return json_encode($aOut);
       }

       // ... other methods of the service

 }

try {
    $output = $server->handle();
    echo $output;
} catch (Exception $e) {
    echo  ($e->getMessage());
    //header('HTTP/1.1 400 BAD REQUEST');
    exit();
}

About the client you can send a json message like this in a post request:

{ 
    "jsonrpc": "2.0", 
    "method": "helloworld", 
    "params": {}, 
    "id": 1 
}

In this post Send json post using php you can see some examples of json request via curl or via Http Zend module.

0
Teun Ouwehand On

For JSON-RPC you can use this one: jsonrpcphp

See example: [Server]

<?php
require_once 'example.php';
$myExample = new example();

// performs some basic operation
echo '<b>Attempt to perform basic operations</b><br />'."\n";
try {
    echo 'Your name is <i>'.$myExample->giveMeSomeData('name').'</i><br />'."\n";
    $myExample->changeYourState('I am using this function from the local environement');
    echo 'Your status request has been accepted<br />'."\n";
} catch (Exception $e) {
    echo nl2br($e->getMessage()).'<br />'."\n";
}

// performs some strategic operation, locally allowed
echo '<br /><b>Attempt to store strategic data</b><br />'."\n";
try {
    $myExample->writeSomething('Strategic string!');
    echo 'Strategic data succefully stored';
} catch (Exception $e) {
    echo nl2br($e->getMessage());
}
?>

[client]

<?php
require_once 'jsonRPCClient.php';
$myExample = new jsonRPCClient('http://jsonrpcphp.org/server.php');

// performs some basic operation
echo '<b>Attempt to perform basic operations</b><br />'."\n";
try {
    echo 'Your name is <i>'.$myExample->giveMeSomeData('name').'</i><br />'."\n";
    $myExample->changeYourState('I am using this function from the network');
    echo 'Your status request has been accepted<br />'."\n";
} catch (Exception $e) {
    echo nl2br($e->getMessage()).'<br />'."\n";
}

// performs some strategic operation, locally allowed
echo '<br /><b>Attempt to store strategic data</b><br />'."\n";
try {
    $myExample->writeSomething('Strategic string!');
    echo 'Strategic data succefully stored';
} catch (Exception $e) {
    echo nl2br($e->getMessage());
}
?>

Source: http://jsonrpcphp.org/?page=example&lang=en