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

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了php – 使用REST服务core_files_upload将文件上传到Moodle脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在开发一个 Android应用程序,它将使用Moodle提供的REST webservice core_files_upload将内容上传到我的Moodle安装中的用户私有文件. core_files_upload采用以下参数:

contextid
component
filearea
ITemid
filepath
filename
filecontent

Moodle Web Services的文档不是很详细,所以我把我可以从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作为上下文 – 由于缺乏文档,我确认这是否正确.当我发布这个我收到错误

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

我已经查看了“moodle / files / externallib.PHP”中定义的core_files_upload的位置,并且当filecontent不存在时会生成错误消息.

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

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

任何人都可以解释为什么我不能成功上传到Moodle?
contextid是用户进行上传用户标识吗?

解决方法

好吧,所以经过一些进一步的阅读和黑客攻击代码后,我可以确认上下文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);

希望这可以帮助任何面临同样问题的人.

脚本宝典总结

以上是脚本宝典为你收集整理的php – 使用REST服务core_files_upload将文件上传到Moodle全部内容,希望文章能够帮你解决php – 使用REST服务core_files_upload将文件上传到Moodle所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。