Drupal Web Services + Json Server + Cake PHP - Part 3

So, here we are going to create a Cake PHP Controller without a Model.
This Controller will perform Calls to a Drupal Web Service as you saw in the previous Articles, the Call will return a Drupal User Object in Json format.

1 - Create your Controller:

<?php
class WilsolutionsServices Controller extends AppController {
   
    var
$uses = array();
    var
$components = array('RequestHandler', 'Session');
       
    private
$api_key = '1641795f7716f5d5a93388aa0843ae3d';
       
    function
getUser() {
       
$args = array(
           
'method'=>'wilsolutions_user.get',
           
'domain'=>'http://your_drupal_server/services/json',
           
// Note: you need to create a Cake PHP View with a form and an input text named "mail", replace with an e-mail if you want.
           
'mail' => $this->data['WilsolutionsServices']['mail']
        );
       
var_dump($this->ApCall($args));
    }
   
    function
ApCall($args) {
       
$webservice     = $args['domain'];
       
$api_key         = $this->api_key;
       
$domain         = $_SERVER['HTTP_HOST'];
       
$timestamp         = (string) $this->Session->time;
       
$nonce             = md5($timestamp);
       
$sid             = $this->Session->_userAgent;
       
$method         = $args['method'];
       
$hash             = hash_hmac('sha256', $timestamp.';'.$domain.';'.$nonce.';'.$method, $api_key);
               
       
$ch = curl_init();
       
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
       
curl_setopt($ch, CURLOPT_POST, 1);
       
curl_setopt($ch, CURLOPT_URL, $webservice);
       
       
$data = array(
           
'method'            => '"'.$method.'"',
           
'hash'                => '"'.$hash.'"',
           
'domain_name'        => '"'.$domain.'"',
           
'domain_time_stamp' => '"'.$timestamp.'"',
           
'nonce'             => '"'.$nonce.'"',
           
'api_key'             => '"'.$api_key.'"',
           
'sessid'             => '"'.$sid.'"',
           
'mail'                => '"'.$args['mail'].'"',
        );

       
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
       
$result = curl_exec($ch);
       
        return
$result;
    }   
}
?>

This is a example using Cake PHP, you can use whatever you want/need: C#, Java, Python, etc...

Now if you call you Controller It will return a User Object from Drupal with Json format.

Some considerations:
- Make sure yours API Key uses your 3rd part app Domain else you will get a "Invalid API Key" message.

Enjoy