From ac23a896e628513279d20ce68703a82d06c54376 Mon Sep 17 00:00:00 2001 From: Luke Carrier Date: Fri, 15 Jun 2018 01:28:30 +0100 Subject: [PATCH] client: initial commit --- README.md | 25 +++++++++++ classes/client.php | 106 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 131 insertions(+) create mode 100644 classes/client.php diff --git a/README.md b/README.md index 6f97fb0..7ab13b2 100644 --- a/README.md +++ b/README.md @@ -14,3 +14,28 @@ A Moodle webservice protocol implementation that allows requests with bodies. It 1. Ensure _Enable web services_ is enabled under _Site administration_ > _Advanced features_. 2. Navigate to _Site administration_ > _Plugins_ > _Web services_ > _Manage protocols_ and enable _Loaded REST protocol_. 3. Assign the _Use Loaded REST protocol_ capability to the desired roles under _Site administration_ > _Users_ > _Permissions_ > _Define roles_. + +## Sample client usage + +```php +use webservice_loadedrest\client; + +$serverurl = '/webservice/loadedrest/server.php'; +$token = '000000000000000000000000000000000000'; +$httpmethod = 'PUT'; +$wsfunction = 'component_function'; +$params = [ + 'myargs' => [ + [ + 'param' => 'look, nesting!', + ], + ], +]; + +$client = new client($serverurl, $token); +try { + var_dump($client->call($httpmethod, $wsfunction, $params)); +} catch (Exception $e) { + var_dump($e); +} +``` diff --git a/classes/client.php b/classes/client.php new file mode 100644 index 0000000..9328758 --- /dev/null +++ b/classes/client.php @@ -0,0 +1,106 @@ +. + +/** + * Loaded REST. + * + * @package webservice_loadedrest + * @author Luke Carrier + * @copyright 2018 Luke Carrier + */ + +namespace webservice_loadedrest; + +use moodle_exception; +use moodle_url; +use webservice_loadedrest\format\format_factory; + +defined('MOODLE_INTERNAL') || die; + +/** + * Test webservice client. + */ +class client { + /** + * Path to server.php, relative to wwwroot. + * + * @var string + */ + protected $serverurl; + + /** + * Authentication token. + * + * @var string + */ + protected $token; + + /** + * I/O format. + * + * @var string|null + */ + protected $formatname; + + /** + * Initialiser. + * + * @param string $baseurl + * @param string $token + * @param string|null $format + */ + public function __construct($baseurl, $token, $format=null) { + $this->serverurl = $baseurl; + $this->token = $token; + $this->formatname = $format ?? format_factory::FORMAT_DEFAULT; + } + + /** + * Call an external function. + * + * @param $httpmethod + * @param $wsfunction + * @param $bodyparams + * + * @return mixed + * + * @throws moodle_exception + */ + public function call($httpmethod, $wsfunction, $bodyparams) { + $format = format_factory::create($this->formatname); + + $getparams = [ + 'wstoken' => $this->token, + 'wsfunction' => $wsfunction, + ]; + $url = new moodle_url($this->serverurl, $getparams); + $headers = [ + 'Content-Type' => $this->formatname, + ]; + $body = $format->serialise($bodyparams); + + $curl = curl_init($url->out(false)); + curl_setopt_array($curl, [ + CURLOPT_CUSTOMREQUEST => $httpmethod, + CURLOPT_HTTPHEADER => $headers, + CURLOPT_POSTFIELDS => $body, + CURLOPT_RETURNTRANSFER => true, + ]); + $result = curl_exec($curl); + + return $format->deserialise($result); + } +}