Hello there, to this Article I am considering that you have already setup your Drupal Services, Json Server and already have a beautiful Cake PHP running...
So, let's code:
1 - Created a Module:
- Folder: wilsolutions_service
- Files: wilsolutions_service.info and wilsolutions_service.module
1.1 - Info file wilsolutions_service.info:
;$Id$
name = "Wilsolutions Service"
description = "Provides services to interact with Users Object."
core = 6.x
dependencies[] = services1.2 - Module file wilsolutions_service.module:
Note: Basically consuming this Web Service will have access to the method: wilsolutions_user.get, It return an User Object using an e-mail as argument.
<?php
/**
* Implementation of hook_help().
*/
function wilsolutions_service_help($section) {
switch ($section) {
case 'admin/help#services_node':
return t('<p>Provides echo methods to services applications. Requires services.module.</p>');
case 'admin/modules#description':
return t('Provides echo methods to services applications. Requires services.module.');
}
}
function wilsolutions_service_perm() {
return array(
'get any user data', 'get own user data',
'update any user data', 'update own user data',
'create new users',
'delete any user', 'delete own user',
);
}
/**
* Check if the user is allowed to get the user data.
*
* @param $uid
* Number. The user ID.
*/
function wilsolutions_user_service_get_access($uid) {
global $user;
return (($user->uid == $uid && user_access('get own user data')) || ($user->uid != $uid && user_access('get any user data')));
}
/**
* Implementation of hook_service()
*/
function wilsolutions_service_service() {
return array(
array(
'#method' => 'wilsolutions_user.get',
'#callback' => 'wilsolutions_user_service_get',
'#access callback' => 'wilsolutions_user_service_get_access',
'#args' => array(
array(
'#name' => 'mail',
'#type' => 'string',
'#description' => t('Array(mail).'),
),
),
'#return' => 'struct',
'#help' => t('Get all user details.')
),
);
}
function wilsolutions_user_service_get($mail) {
$account = user_load(array('mail'=>$mail));
if (empty($account)) {
return services_error(t('There is no user with such e-mail.'), 404);
}
// Everything went right.
return $account;
}
function wilsolutions_service_echo($message) {
$return = new stdClass();
$return->sessid = session_id();
$return->message = $message;
return $return;
}
?>