简体   繁体   中英

send request with data from external server to Moodle

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.

You can use web services

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

Here are some brief instructions

  • Enable web services /admin/search.php?query=enablewebservices
  • Enable rest protocol /admin/settings.php?section=webserviceprotocols
  • Add a service /admin/settings.php?section=externalservices
  • -- add short name = yourserviceshortname
  • -- enable = true
  • -- save changes
  • Click on 'functions for the service'
  • -- add any required functions
  • Create a role - /admin/roles/manage.php
  • -- Authenticate user / system
  • -- Add capability - webservice/rest:use
  • Create a user and add to the role
  • Create a token for the user /admin/settings.php?section=webservicetokens

Then in php you can do something like this:

$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);

For a complete list of available functions see /admin/webservice/documentation.php

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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