Categories
PHP

Web Services – Yet Another RPC Framework

A web service is an interfaceless application that can be called to process information. In PHP, a web service can provide the functionality remotely on a web server.

PHP.net listed the following web services in its documentation:

  1. OAuth
    It is an authorization protocol built on top of HTTP which allows applications to securely access data without having to store usernames and passwords.
  2. SOAP
    It is an RPC-style service, PHP has a built-in extension for both client and server.
  3. Yar (Yet Another RPC Framework)
    This PECL extension is not bundled with PHP.
  4. XML-RPC
    This extension is experimental and not compiled by default, you need to add --with-xmlrpc to your configure line when you compile PHP.

YAR – Yet Another RPC Framework

Enable Yar RPC on Windows (Xampp)

Enable Yar-RPC on Windows (XAMPP)
  1. Visit https://pecl.php.net/package/yar
  2. Choose the latest DLL download link (2.2.1 at the time of writing this tutorial)
  3. On the next page, choose a download link according to your PHP version. For example, choose “8.1 Thread Safe (TS) x64” if your Windows is 64-bit and installed the PHP version is 8.1.
  4. Extract the downloaded zip archive.
  5. Copy php_yar.dll file and paste it into the xampp/php/ext folder.
  6. Open xampp/php/php.ini file and add the following line
    extension=php_yar.dll, and save the changes.
  7. Restart the Apache web server

For more information, visit https://php.net/manual/install.pecl.php.

Creating a Web Service with YAR

To provide Web Service with Yar, you have to instantiate a class, the preceding code creates a RPC server for a simple Add, Subtract, and Multiply Web Service:

<?php
 /* this page can be accessed by http://locahost/yar_server.php */
 class Mathematics {
 /**
  * BrainBell.com
  * Add two numbers
  * @param interge 
  * @return interge
 */
  public function sum($a, $b) {
   return $this->_sum($a, $b);
  }

  /**
   * BrainBell.com
   * Subtract
  */
  public function subtract($a, $b) {
   return $a - $b;
  }

  /**
   * BrainBell.com
   * Multiply
  */
  public function multiply($a, $b) {
   return $a * $b;
  }

  /**
   * Protected methods will not be exposed
   * @param interge 
   * @return interge
  */
  protected function _sum($a, $b) {
   return $a + $b;
  }
 }

 $object = new Mathematics();
 $server = new Yar_Server($object);
 $server->handle();

To set up an HTTP RPC Server:

  1. $object = new Mathematics();
    Initiate the class you want to register as the web service.
  2. $server = new Yar_Server($object);
    Set up an RPC Server, all the public methods of $object will be registered as an RPC service.
  3. $server->handle();
    Starts an RPC server, and is ready to accept RPC requests.

When you access the server URL in the browser. The above code will output something like this:

Consuming a Web Service with YAR

Querying the Web Service is rather easy. Just instantiate the yar_client class (providing the Yar Server URL), then call the object methods with all the parameters. You receive the result of the Web Service call in return or an error message:

<?php
 /* this page can be accessed by http://locahost/yar_client.php */

 $client = new yar_client("http://localhost/example.php");
 
 $sum = $client->sum(23, 19); // Mathematics::sum
 var_dump($sum); //int(42)

 // OR use call method
 $sub = $client->call('subtract', [42, 18]);
 // Calls Mathematics::subtract
 
var_dump($sub); //int(24)

Async Calls to RPC Server

Use the Yar_Concurrent_Client class for async calls to the RPC server:

<?php
 /* this page can be accessed by http://locahost/yar_client_async.php */
 function cbFunc($result, $info) {
  echo $info['method'] , ": ", $result , '<br>';
 }
 
 $url = 'http://localhost/example.php';
 $sum = array(24, 26);
 $sub = array(26, 24);
 $mul = array(10, 10);

Yar_Concurrent_Client::call($url, 'sum', $sum, "cbFunc");
Yar_Concurrent_Client::call($url, 'subtract', $sub, "cbFunc");
Yar_Concurrent_Client::call($url, 'multiply', $mul, "cbFunc");

/* sent all request and wait for response */
Yar_Concurrent_Client::loop();

For more information, visit https://php.net/manual/class.yar-concurrent-client.php.


Communicating with Servers: