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.

Matt Bearson
  • 228
  • 5
  • 19
  • You should ask a more specific question showing that you made some efforts to search for answers by yourself first : [How do I ask a good question ?](http://stackoverflow.com/help/how-to-ask) – Ivan Gabriele Jun 22 '16 at 09:59
  • actualy I don`t know any possibilities to do that... I know how send data from Moodle to external server, but how from external server to Moodle... – Matt Bearson Jun 22 '16 at 10:07

1 Answers1

1

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

Russell England
  • 9,436
  • 1
  • 27
  • 41