Need help with custom service method

Events happening in the community are now at Drupal community events on www.drupal.org.
Posted by christothetopher on November 18, 2010 at 2:00pm

I love the services module and everything I've been able to do with it so far. That being said, I can't do everything I want to achieve with the module as-is. So, I decided to extend the services module and therein lies the problem!

I created a new directory inside the services subfolder, userprofile_service. I've emulated everything I've seen inside the other folders (inc, info and module files) and wrote my custom code to interact with drupal.

I then wrote a small client to attempt to use the new services and I'm getting the following error: ( [faultCode] => -32601 [faultString] => Server error. Requested method userprofile.save not specified. )

I'm guessing that I've missed a step somewhere and need to register the method within drupal or the xmlrpc server somehow?

Any help, a link to a tutorial or pretty much anything would be greatly appreciated!

Comments

Here's an example

Posted by waldmanm on November 18, 2010 at 5:19pm

Here's a short example of a custom module I'm using. I actually put it in the 'modules' directory as a standard module, not under services (perhaps that's the problem?). The module is called user_gems_service and it updates a very specific hidden user profile field (that user.save does not update), which I'm using in a Flex app. It's not general purpose at all, but is OK as a short example of a custom services module.

Once you place your module in the 'modules' directory, enable the module and, if you're using API keys, clear the cache and add your method to the API key. Until you do that, you won't be able to call the method.

user_gems_service.info:

name = User Gems Service
description = Provides a service for updating user Gems balance.
package = GEM InSight
dependencies[] = services
dependencies[] = profile
core = 6.x

user_gems_service.module:

<?php
/<strong>
*
Implementation of hook_help().
*/
function
user_gems_service_help($path, $arg) {
switch (
$path) {
case
'admin/help#user_gems_nodes':
return
t('<p>Provides services for updating user gems balance.</p>');
case
'admin/modules#description':
return
t('Provides services updating user gems balance.');
}
}

/</
strong>
*
Implementation of hook_perm().
*/
function
user_gems_service_perm() {
return array(
'update user gems balance from remote');
}
/**
* Implementation of hook_service().
*/
function user_gems_service_service() {
return array(

// user_gems.update
array(
'#method' => 'user_gems.update',
'#callback' => 'user_gems_service_update',
'#access arguments' => array('update user gems balance from remote'),
'#file' => array('file' => 'inc', 'module' => 'user_gems_service'),
'#args' => array(
array(
'#name' => 'balance',
'#type' => 'int',
'#description' => t('New Gems balance.'),
),
),
'#return' => 'bool',
'#help' => t('This method update gems balance for a user.'),
),

);
}
?>

user_gems_service.inc:

<?php
/**
* Set user's gems balance. If previous balance existed it is updated.
*
* @param $balance
* A numeric balance. Should be >= 0.
* @return
* TRUE if balance updated, FALSE otherwise
*/
function user_gems_service_update($balance) {

global
$user;

// Verify balance is set
if ( ! isset($balance) ) return FALSE;

// Find out 'profile_gems' field number
$result = db_query("SELECT fid, name FROM {profile_fields} WHERE name = 'profile_gems'");
$record = db_fetch_object($result);
if ( empty(
$record) || (! $record->fid) ) {
return
FALSE;
}
$gems_fid = $record->fid;

// Delete blance if it exists
db_query("DELETE FROM {profile_values} WHERE fid = %d AND uid = %d", $gems_fid, $user->uid);

// Insert new balance (TODO: coinsider using drupal_write_record())
$result = db_query("INSERT INTO {profile_values} (fid, uid, value) VALUES (%d, %d, '%s')", $gems_fid, $user->uid, $balance);

return (
$result ? TRUE : FALSE);
}
?>

Hope this helps.
Micah

Services

Group organizers

Group categories

Group notifications

This group offers an RSS feed. Or subscribe to these personalized, sitewide feeds:

AltStyle によって変換されたページ (->オリジナル) /