0

is it possible to send POST request from external server to Moodle and then, already in Moodle, doing some actions with data and save to DB(DB table created by local plugin). Have any possibilities to do that? Thanks all for help.

4

1 回答 1

1

您可以使用网络服务

https://docs.moodle.org/dev/Web_services

这里有一些简短的说明

  • 启用网络服务/admin/search.php?query=enablewebservices
  • 启用休息协议/admin/settings.php?section=webserviceprotocols
  • 添加服务/admin/settings.php?section=externalservices
  • -- 添加短名称 = yourserviceshortname
  • -- 启用 = 真
  • - 保存更改
  • 点击“服务功能”
  • -- 添加任何需要的功能
  • 创建角色 -/admin/roles/manage.php
  • -- 验证用户/系统
  • -- 添加能力-webservice/rest:use
  • 创建用户并添加到角色
  • 为用户创建令牌/admin/settings.php?section=webservicetokens

然后php你可以做这样的事情:

$tokenurl = 'http://[url]/login/token.php?username=xxx&password=xxx&service=yourserviceshortname';

$tokenresponse = file_get_contents($tokenurl->out(false));

$tokenobject = json_decode($tokenresponse);

if (!empty($tokenobject->error)) {
    echo $tokenobject->error;
    die();
}

$functionurl = 'http://[url]/webservice/rest/server.php';
$functionurl .= '?wstoken=' . $tokenobject->token;
$functionurl .= '&wsfunction=functionname';
$functionurl .= '&moodlewsrestformat=json';
$functionurl .= '&param1=xxx';
$functionurl .= '&param2=yyy';

$functionresponse = file_get_contents($functionurl);

$object = json_decode($functionresponse);

var_dump($object);

有关可用功能的完整列表,请参阅/admin/webservice/documentation.php

于 2016-06-22T13:45:20.667 回答