使用REST服务core_files_pload将文件上载到Moodle


Uploading file to Moodle using the REST Service core_files_upload

我正在开发一款Android应用程序,该应用程序将使用Moodle提供的REST Web服务core_files_pload将内容上传到Moodle安装中的用户私人文件中。core_files_pload采用以下参数:

contextid
component
filearea
itemid
filepath
filename
filecontent

Moodle Web Services的文档不是很详细,所以我已经从Moodle论坛和谷歌搜索中拼凑出了我能做的,但我觉得我已经走到了死胡同。从我看到的例子中,参数采用以下值:

contextid: int - not sure whether this is the userid
component: "user"
filearea: "private"
itemid: 0
filepath: "/"
filename: "filename.jpg"
filecontent: base64 encoding of file

我使用我的userid作为上下文——由于缺乏文档,我不确定这是否正确。当我发布这篇文章时,我收到了错误:

{"exception":"moodle_exception","errorcode":"nofile","message":"File not specified"}

我已经查看了在"moodle/filess/externallib.php"中定义core_files_pload的位置,并且当文件内容不存在时会生成此错误消息。

我已经尝试过发布到一个测试脚本,我可以成功地在Moodle服务器上创建基于base64编码的图像,例如:

<?php
file_put_contents('MyFile.jpg', base64_decode($_POST['filecontent']));

有人能解释为什么我上传到Moodle不成功吗
contextid是上传用户的userid吗?

好的,所以在进一步阅读和破解代码后,我可以确认上下文id不是用户id,而是从用户id派生的。我在Moodle中没有找到任何获取上下文id的方法,所以我创建了自己的上下文id。一旦我有了用户的上下文id,上传就成功了。

define('AJAX_SCRIPT', true);
define('NO_MOODLE_COOKIES', true);
require_once(dirname(dirname(__FILE__)) . '/config.php');
require_once($CFG->dirroot . '/webservice/lib.php');
echo $OUTPUT->header();
// authenticate the user
$token = required_param('token', PARAM_ALPHANUM);
$webservicelib = new webservice();
$authenticationinfo = $webservicelib->authenticate_user($token);
// check the user can manage his own files (can upload)
$context = context_user::instance($USER->id);
$result = array("contextid"=>$context->id);
echo json_encode($result);

希望这能帮助到其他面临同样问题的人。

  • contextlevel:将文件放入的上下文级别,(块、课程、课程猫、系统、用户、模块)

  • instanceid:与上下文级别关联的项目的实例id

如果指定contextlevel=user,则instanceid必须是DB中的用户id。

也许有人能发布一个正确的解决方案。

我将使用Moodle的Web服务API(core_files_pload)和作为参数的UserID。

$fileinfo = array(
'contextid' => null,
'component' => 'user',
'filearea' => 'draft',
'itemid' => 0,
'filepath' => '/',
'filename' => 'sample.txt',
'filecontent' => base64_encode("Hello Word!"),
'contextlevel' => 'user',
'instanceid' => $userid,
);

错误是"未指定文件"-Moodle将此文件上传到临时目录,但随后停止并返回错误消息

在花了三天时间研究moodle core_files_pload函数后,我设法找到了这个最佳解决方案。希望这对将来的人有所帮助。

<?php
 $token = 'Put your token here';
 $domainname = 'Put your app url here';
 $restformat = 'json';
require_once('curl.php'); //Download this file
$curl = new curl;
$params = array(
    'component' => 'user',
    'filearea' => 'draft',
    'itemid' => 0,
    'filepath' => '/',
    'filename' => 'moodle.PNG',
    'filecontent' => base64_encode('/images/moodle.PNG'),
    'contextlevel' => 'user',
    'instanceid' => $userid,
);
/// UPLOAD IMAGE - Moodle 3.10+ and later
$functionname = 'core_files_upload';
$serverurl = $domainname .'/webservice/rest/server.php' . '?wstoken=' . $token .'&wsfunction=' . $functionname;
$restformat = ($restformat == 'json') ?
'&moodlewsrestformat=' . $restformat : '';
print_r($params);
printf("'n");
printf("'n");
$resp = $curl->post($serverurl . $restformat, $params);
$resps = json_decode($resp);
print_r($resps);
printf("'n");

还要检查这个https://moodle.org/mod/forum/discuss.php?d=275796我已经测试过了,它正在按预期工作。