how to define parameters for external method calls

how to define parameters for external method calls

by Murad Jamal -
Number of replies: 5

after looking at your webservice client example, I tried to make another example (for self training purposes), using the method: moodle_user_create_users, I have two questions:

1) why is moodle_user_create_users method (and all other external methods) named like this ? i mean why is it different from its real name creat_users which is defined in the class path : moodle/user/externallib.php ?

 

2) I looked at the function create_users_parameters which builds up parameters for create_users method, I need an example of a valid $params variable to put into my webservice client, this $params  satisfies create_users function giving that the client will create two users.

thank you in advance

Average of ratings: -
In reply to Murad Jamal

Re: how to define parameters for external method calls

by RICHARD gillette -

I am using LWP to post through the REST web service and when I post to make a new user it looks something like this...

The param hash looks like this

$params = {
            'wstoken' => '99832h9h239h23890h',
            'wsfunction' => 'moodle_user_create_users',
            'users[0][username]' => 'theUserName',
            'users[0][firstname]' => 'theFirstName',
            'users[0][lastname]' => 'theLastName',
            'users[0][email]' => 'TheEmail',
            'users[0][password]' => 'soOnAndSoForth',
        };

Then I do an LWP post like so

$response = $userAgent->post($theURL, $params);

and the xml you get back goes into $response which you can then parse or do w/e

if you want to do multiple users at once then try incrementing the user array like - 'users[1][username]' => 'theUserName', though I am creating one user at a time and haven't tried that, i see no reason why it wont work.

Hope that helps

In reply to RICHARD gillette

Re: how to define parameters for external method calls

by RICHARD gillette -

Forgot to mention, I'm doing all of this in PERL if that matters

In reply to RICHARD gillette

Re: how to define parameters for external method calls

by Murad Jamal -

Richard, according to the structure of the function that is responsible for parameters (the function is defined in user/externallib.php file line 35), if I to follow this function exactly, your $params structure will not work, because the main function contains nested arrays ... I really find it hard to build paramters from these functions and many other similar functions, is there a specific approach that simplifies this process for us ?

In reply to Murad Jamal

Re: how to define parameters for external method calls

by richard gillette -

I agree it is hard to build paramters. I spent about an hour today trying to find out how to build a parameter for get_courses and create_courses finally comming up with

get_courses = 'options[ids][0]' => '$id',

create_courses = 'courses[0][fullname]' => '$name',

                            'courses[0][shortname]' => '$name',

I used the externallib files too to try and figure out what format it wanted my paramter hash and that is what finally worked.


If you send me a list of the external functions you want to use I can share with you what I have come up with but I agree with you that it is very hard to get things properly formatted and that my solutions don't look like they will work.

 

Richard Gillette

www.OmniBond.com

In reply to richard gillette

Re: how to define parameters for external method calls

by Murad Jamal -

thank you Richard for your help.