-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2ef71e6
commit ac23a89
Showing
2 changed files
with
131 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
<?php | ||
// This file is part of Moodle - http://moodle.org/ | ||
// | ||
// Moodle is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// Moodle is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU General Public License | ||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
/** | ||
* Loaded REST. | ||
* | ||
* @package webservice_loadedrest | ||
* @author Luke Carrier <[email protected]> | ||
* @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); | ||
} | ||
} |