Moodle:从网络服务获取用户图片

Moodle: get user picture from webservice

通过使用 Moodle Web 服务 (REST) 调用 core_user_get_users_by_field 我成功获取了我的用户详细信息。返回的字段之一是 profileimageurl,它 link 指向我的用户个人资料图片,看起来像这样:

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

不幸的是,link 仅在我已经登录的浏览器中有效,否则它将重定向到标准用户图标(灰色匿名脸)。

因此,为了从使用 Moodle Web 服务的客户端应用程序中获取实际图片,我想我必须调用 core_files_get_files 并传递正确的值。所以我尝试将 link 重新映射为这样调用参数:

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

但我得到的是:

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

参数在形式上似乎是正确的(否则我会得到异常)但是我只得到空的响应,告诉我有些值不正确。

好的,我找到了问题的解决方案。我在这里发布答案也是因为周围没有太多关于 Moodle 网络服务的信息...

首先,core_files_get_files 不是这样...它只会显示文件信息,它不会给你实际的文件内容(二进制)。

幸运的是,当从外部客户端应用程序调用时,可以使用等效的 URL:

http://my_moodle_server/网络服务/pluginfile.php

它接受与 http://my_moodle_server/pluginfile.php 相同的 parameters/format,此外您还可以传递您的令牌以进行 Web 服务身份验证。

所以 profileimageurlcore_user_get_users_by_field 返回的字段看起来像这样:

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

可以变成

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

另请注意,需要附加 ?token= 参数