简体   繁体   English

使用 REST 服务 core_files_upload 上传文件到 Moodle

[英]Uploading file to Moodle using the REST Service core_files_upload

I am developing an Android app, which will upload content to a user's private files in my Moodle installation using the REST webservice core_files_upload provided by Moodle.我正在开发一个 Android 应用程序,它将使用 Moodle 提供的 REST webservice core_files_upload 将内容上传到我的 Moodle 安装中的用户私人文件。 core_files_upload takes the following parameters: core_files_upload 采用以下参数:

contextid
component
filearea
itemid
filepath
filename
filecontent

The documentation for the Moodle Web Services isn't very detailed so I have pieced together what I can from the Moodle forums and Google searches, but I feel I have come to a dead end. Moodle Web Services 的文档不是很详细,所以我从 Moodle 论坛和谷歌搜索中拼凑了我所能做的,但我觉得我已经走到了死胡同。 From examples I have seen that the parameters take the following values:从示例中我看到参数采用以下值:

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

I am using my userid as the contextid - I'm usnsure whether this is correct due to lack of documentation.我使用我的用户 ID 作为上下文 ID - 由于缺乏文档,我不确定这是否正确。 When I post this I receieve the error:当我发布这个时,我收到错误:

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

I have looked at where core_files_upload is defined in "moodle/files/externallib.php" and this error message is generated when filecontent is not present.我查看了在“moodle/files/externallib.php”中定义 core_files_upload 的位置,当文件内容不存在时会生成此错误消息。

I have tried posting to a test script and I can successfully create the image on the Moodle server based on the base64 encoding eg:我尝试发布到测试脚本,我可以基于 base64 编码在 Moodle 服务器上成功创建图像,例如:

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

Can anyone shed light on why I am being unsuccessful uploading to Moodle?谁能解释为什么我上传到 Moodle 失败?
Is the contextid the userid of the user doing the upload? contextid 是进行上传的用户的 userid 吗?

Ok, so after a bit of further reading and hacking around with code I can confirm that the context id is not the user id, but is derived from the user id.好的,所以在进一步阅读和修改代码之后,我可以确认上下文 id 不是用户 id,而是从用户 id 派生的。 I have not found any way in Moodle to get the the context id, so I have created my own.我还没有在 Moodle 中找到任何获取上下文 ID 的方法,所以我创建了自己的。 Once I had the context id for the user, the upload worked.一旦我获得了用户的上下文 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);

Hope this helps anyone else who faces the same problem.希望这可以帮助任何面临同样问题的人。

  • contextlevel : The context level to put the file in, (block, course, coursecat, system, user, module) contextlevel :放置文件的上下文级别,(块,课程,课程猫,系统,用户,模块)

  • instanceid : The Instance id of item associated with the context level instanceid :与上下文级别关联的项目的实例 ID

If you specify contextlevel=user then instanceid must be the user id in DB.如果您指定contextlevel=userinstanceid必须是数据库中的用户 ID。

maybe can someone post a correct solution.也许有人可以发布正确的解决方案。

I will use the Webservice API (core_files_upload) from Moodle and the UserID as Parameter.我将使用 Moodle 的 Webservice API (core_files_upload) 和 UserID 作为参数。

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

The error is ever "file not specified" - Moodle upload this file to a temp Directory, but then stopped and answer with the error message错误是“未指定文件”- Moodle 将此文件上传到临时目录,但随后停止并回答错误消息

After spending three days researching on the moodle core_files_upload function I managed to come up with this optimal solution.在研究了moodle core_files_upload 函数三天后,我设法想出了这个最佳解决方案。 Hope this will help someone in future.希望这会在将来对某人有所帮助。

<?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");

Also check this https://moodle.org/mod/forum/discuss.php?d=275796 I have tested and it's working as expected.还要检查这个https://moodle.org/mod/forum/discuss.php?d=275796我已经测试过并且它按预期工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM