amb*_*ide 4 php rest android web-services moodle
我正在开发一个Android应用程序,它将使用Moodle提供的REST Web服务core_files_upload将内容上传到我的Moodle安装中的用户的私人文件中。core_files_upload采用以下参数:
contextid
component
filearea
itemid
filepath
filename
filecontent
Moodle Web服务的文档不是很详细,因此我将Moodle论坛和Google搜索中的内容拼凑而成,但是我觉得自己已经走到了尽头。从示例中,我看到这些参数采用以下值:
contextid: int - not sure whether this is the userid
component: "user"
filearea: "private"
itemid: 0
filepath: "/"
filename: "filename.jpg"
filecontent: base64 encoding of file
我将我的用户ID用作上下文ID-由于缺少文档,我不确定这是否正确。当我发布这个我收到错误:
{"exception":"moodle_exception","errorcode":"nofile","message":"File not specified"}
我查看了在“ moodle / files / externallib.php”中定义了core_files_upload的位置,当文件内容不存在时会生成此错误消息。
我尝试过发布到测试脚本,并且可以基于base64编码在Moodle服务器上成功创建图像,例如:
<?php
file_put_contents('MyFile.jpg', base64_decode($_POST['filecontent']));
谁能阐明为什么我无法成功上传到Moodle?
contextid是执行上载的用户的userid吗?
好的,所以在进一步阅读和修改代码后,我可以确认上下文ID不是用户ID,而是从用户ID派生的。我没有在Moodle中找到获取上下文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);
希望这对其他遇到相同问题的人有所帮助。
| 归档时间: | 
 | 
| 查看次数: | 2337 次 | 
| 最近记录: |