lviggiani
lviggiani

Reputation: 6066

Moodle: get user picture from webservice

By using Moodle Web Service (REST) call core_user_get_users_by_field I successfully get my user details. One of the returned fields is profileimageurl that links to my user profile picture and look like this:

http://my_moodle_server/pluginfile.php/89/user/icon/f1

Unfortunately that link works only in a browser where I have already logged in, otherwise it will redirect to a standard user icon (the grey anonymous face).

So in order to get the actual picture form a client app that is using moodle web services I think I have to call core_files_get_files and pass correct values. So I tried to remap that link to calls paramenters like this:

contextid: 89
component: "user"
filearea: "icon"
itemid: 0
filepath: "/"
filename: "f1.png" (also tryed without .png)
and of course my valid token

but all I get is:

{"parents":[],"files":[]}

Parameters seem to be formally correct (otherwise I would get an exception) however I only get empty response which tells me that some values are not correct.

Upvotes: 2

Views: 3018

Answers (1)

lviggiani
lviggiani

Reputation: 6066

Ok I found the solution to my problem. I'm posting the answer here also because there's not much information about Moodle web service around...

First of all, core_files_get_files is not the way... it will only show files information, it won't give you the actual file contents (binary).

Fortunately, there's an equivalent URL to be used when calling from external client app:

http://my_moodle_server/webservice/pluginfile.php

It accepts the same parameters/format as http://my_moodle_server/pluginfile.php and in addition you can also pass your token for web service authentication.

So profileimageurl field returned by core_user_get_users_by_field which looks like this:

http://my_moodle_server/pluginfile.php/89/user/icon/f1

can be turned into

http://my_moodle_server/webservice/pluginfile.php/89/user/icon/f1?token=my_web_service_token

Also note that appending ?token= parameter is required

Upvotes: 4

Related Questions