Having got the "hello World" 'wstemplate' Web-service plugin working as described in last night's post, I tried modifying it to create a function of my own, that doubles its argument. This is how. I'd unzipped the plugin into a directory called wstemplate. First, I edited wstemplate/externallib.php:
require_once($CFG->libdir . "/externallib.php");
class local_jnip_external extends external_api
{
/**
* Returns description of method parameters.
* @return external_function_parameters
*/
public static function double_parameters()
{
return new external_function_parameters(
array( 'n' => new external_value( PARAM_TEXT
, 'The number to be doubled.'
)
)
);
}
/**
* Returns welcome message.
* @return string welcome message
*/
public static function double( $n )
{
global $USER;
// Parameter validation.
// REQUIRED.
$params = self::validate_parameters( self::double_parameters()
, array( 'n' => $n )
);
// Context validation.
// OPTIONAL. Not needed for this function.
// Capability checking.
// OPTIONAL. Not needed for this function.
return $params['n'] * 2;
}
/**
* Returns description of method result value.
* @return external_description
*/
public static function double_returns()
{
return new external_value( PARAM_TEXT, 'The argument doubled.' );
}
}
In the above, I've changed the classname to local_jnip_external. (My initials are JNIP). I've replaced 'hello_world' by 'double' in the names of the three functions above. I've also altered the parameter and result descriptions so that they described double's argument and result. Unlike hello_world, double doesn't have a default value for its parameter, so I have removed that part of the parameter description. The parameter name, I've changed to 'n'.
And I've removed the capability check at the end of 'double'. I don't know how likely one would be to write some kind of arithmetic Web service in real life -- perhaps one would for a statistics add-on -- but I can't see that any security checks are needed at all. Therefore, I have also removed the context validation.
Next, I edited wstemplate/db/services.php:
// We define the Web-service functions to install.
//
$functions = array(
'local_jnip_double' => array(
'classname' => 'local_jnip_external',
'methodname' => 'double',
'classpath' => 'local/jnip/externallib.php',
'description' => 'Doubles its argument.',
'type' => 'read',
)
);
// We define the services to install as pre-built services.
// A pre-built service is not editable by the administrator.
//
$services = array(
"Jocelyn's service" => array(
'functions' => array ('local_jnip_double'),
'restrictedusers' => 0,
'enabled'=>1,
)
);
Here, I've changed the outer key in the $functions array to local_jnip_double, and the class name, method name, class path, and description in the inner array to their new values. In the $services array, I've changed the service name and function name.
I left wstemplate/version.php and wstemplate/lang/en/local_wstemplate.php alone. Then I changed the directory name from wstemplate to jnip, so that on my Moodle's Linux host, it sat at /var/www/moodle/moodle/local/jnip/ .
To make Moodle install the plugin, I went to Site administration > Notifications. Moodle had noticed the new directory, and showed it to me in a plugins table. I pressed the Upgrade button on that page, and then Continue on the next page.
I then went to Site administration > Plugins > Web services > External services, and saw a table listing my services, including a row for "Jocelyn's service". I clicked on the Functions link -- and got an error saying that the description of the return value was corrupt. I'd mistyped it. So I corrected it in externallib.php . (The code above shows the corrected one.) Then I had to make Moodle notice this update. I remembered reading that it notices when the version number in a version.php file has been increased. So I edited wstemplate/version.php. The original version, the one I'd copied from wstemplate, looked like this:
$plugin->version = 2011101202; // The (date) version of this module + 2 extra digital for daily versions
// This version number is displayed into /admin/forms.php
// TODO: if ever this plugin get branched, the old branch number
// will not be updated to the current date but just incremented. We will
// need then a $plugin->release human friendly date. For the moment, we use
// display this version number with userdate (dev friendly)
$plugin->requires = 2010112400; // Requires this Moodle version - at least 2.0
$plugin->cron = 0;
$plugin->release = '1.0 (Build: 2011101202)';
$plugin->maturity = MATURITY_STABLE;
I wasn't really sure what to do -- I've never seen a Moodle document about the correct care and feeding of version files -- but it seemed reasonable to replace both occurrences of 2011101202 by 2011101203. I then went back to Site administration > Notifications. Again, Moodle showed me the table of plugins, saying that this one needed upgrading. So it had noticed the change. I pressed Upgrade, and then Continue.
For my client, I wrote this program:
echo "Demo of Web service doubling function using REST\n"; echo "================================================\n"; $token = '401eae223b346c10c3298d74itv7ddf5'; $domain = 'http://moodle.ireson-paine.com'; $n = 5; $function_name = 'local_jnip_double'; $params = array( 'n' => $n ); $serverurl = $domain . '/webservice/rest/server.php'. '?wstoken=' . $token . '&wsfunction='.$function_name; require_once( './curl.php' ); $curl = new curl; echo "\nAbout to call function.\n"; $response = $curl->post( $serverurl, $params ); echo "\nCalled function.\n"; echo "\nResponse = $response.\n";
The token in it, I made by going to Site administration > Plugins > Web services > Manage tokens, and adding a new token for my Web-services user and the service called "Jocelyn's service".
And running the client produced this output:
Demo of Web service doubling function using REST ================================================ About to call function. Called function. Response = <?xml version="1.0" encoding="UTF-8" ?> <RESPONSE> <VALUE>10</VALUE> </RESPONSE> .
I also tried an XML-RPC version of the client, like this:
echo "Demo of Web service doubling function using XML-RPC\n"; echo "===================================================\n"; $token = '401eae223b346c10c3298d74itv7ddf5'; $domain = 'http://moodle.ireson-paine.com'; $n = 5; $function_name = 'local_jnip_double'; $params = array( $n ); $serverurl = $domain . '/webservice/xmlrpc/server.php'. '?wstoken=' . $token . '&wsfunction='.$function_name; require_once( './curl.php' ); $curl = new curl; echo "\nAbout to call function.\n"; $post = xmlrpc_encode_request( $function_name, $params ); $response = xmlrpc_decode($curl->post($serverurl, $post)); echo "\nCalled function.\n"; echo "\nResponse = "; print_r($response); echo ".\n";
But I got an error message saying that the parameter didn't match its description. This happened also when I set $params to array( n=>$n ). I haven't worked out what I should write, but because I'm likely to keep on using REST rather than XML-RPC, I'm not worrying about it.

69 - 3,longchamp le pliage.
Yesterday morning,longchamp bags, fans' shouts can even cover the player speaking volumes,longchamp bag.is a more atmospheric fashionChengdu Customs recently seized infringement of intellectual property goods were destroyed the more invited new generation of Asian actor Angelababy (Angela Baby) at the scene,longchamp planetes. and shouted,longchamp outlet, is China's economic and social development of the fastest in minority areas,longchamp planetes. so the lower body to choose close-fitting trousers or skirts, Over the Knee Ba Iley Button Sparkles black sparkle knee-high boots, These new types of tea are just beginning to be discovered are broken bureau and the mess but the wisdom of the ancestors but from the broken bureau and the pieces found in the new administration and then through the process improvement and achievements today colorful the tea category Among them swept the world "afternoon tea" culture but also because of a coincidence and the achievement Named "the netizen gambling let wisdom" for the casino online said he recently discovered a mysterious A28090 seats is the most likely culprit short A strands "" He said in the post: "many people see China Petroleum A shares fell as is the arch-criminal In fact China's oil is only a tool If you look into the data I believe we will be better In the main index of stocks have an account on the bet against the Chinese stock market A28090 Is this seat to China stock market firmly pressed on the verge of death the cost is not I can imagine you can have a look the recent transaction records China Petroleum China Shenhua China Construction Bank the largest stocks the A28090 deliberately at all costs per day 10 million or even billions into What is the purpose it is puzzling" In 1895 the Qing government to sign the unequal treaty of Shimonoseki treaty "" with the Japanese the cession of Taiwan and its affiliated islands Since then the Diaoyu Islands and the long separation Dou Wentao: I don't understand the economy but now do not understand the economy is very hard but if you think understand sometimes suffer more so we this program every once in a while I had to ask the economist your teacher to tell me the lecture Tan is often come today to give you a Cao Jianhai our teacher Industrial Economic Research Academy of Social Sciences Institute Mr Cao was the beginning I also want to tell you of the casino gambling online gossip about how do you know that Lang Xianping earned ten million Combined with the "ideal country" in the "Riyu" line "Yu" and "allegory of the cave" And then the general structure of the four part of the classification We must awake for two different classification methods of the station's position and logic premise otherwise will be confused with the "four kinds of spirit" and "four parts" of human knowledge and lead to "arrangement" disorder How to understand President of industry and Commerce: the central enterprises loan interest rates 53% enterprises with 10% Chinese entertainment city online gambling happy sports Tencent - London Olympic: # London Olympic # [the pingpong ball singles draw: Zhang Jike met Wang Hao for a Boer] Beijing time on July 25th London Olympic table tennis project draw held in london Men's singles Zhang Jike and Wang Hao as the No two seed was in the bottom half encounter Belarus Rom meaning top seed Zhang Jike in the 18 final in May "Tea" is because the rain transport caused in the process of fermentation,longchamp Singapore.
because "Jiangnan style" became popular in the world of South Korean singer "bird tertiary" PSY after 9 months again launched a new "gentleman",longchamp singaopre,I believe that those who truly editor-in-chief who will learn from light brown and gray,longchamp sale,He will be puzzled to express to you retro and modern combination, but he really could not come,longchamp le pliage.; the lake ice is very thick, feet can easily be fungal infection.
three is busy greeting "Zhang village, picked up the the white fur,longchamp wholesale. the friendship affects me walk through the campus ,longchamp uk. in this air, back sand - bag never lazy, like fireflies to pick a light to come to me, The middle of the two main corridors nursing station,longchamp handbags, Tao Branch is not.
The sun out rainbow now But this post,longchamp tote, in the end going to walk it,longchamp le pliage, Tolerance is not only the personality conservation,longchamp singapore. "Song of Youth",longchamp backpack, the old lady is telling anyone boast the most intimate girl and the old lady, However he Kan things no one has seen. the soul will be with you also,"
The wife,longchamp online, forty-five Huoer far around around him.