我在 Moodle 3.3 中实现了一个简单的 Web 服务客户端并测试了一些函数。出于某种原因,这似乎不起作用。
<?php
$token = '.......';
$domainname = 'http://localhost/moodle';
$functionname = 'core_course_get_contents';
$cid = 5;
/// SOAP CALL
$serverurl = $domainname . '/webservice/soap/server.php'. '?wsdl=1&wstoken=' . $token;
////Do the main soap call
$client = new SoapClient($serverurl);
try {
$resp = $client->__soapCall($functionname, array($cid));
} catch (Exception $e) {
print_r($e);
}
if (isset($resp)) {
print_r($resp);
}
我不断收到以下错误:
SoapFault Object ( [message:protected] => Invalid parameter value detected | ERRORCODE: invalidparameter [string:Exception:private] => [code:protected] => 0 [file:protected] => /var/www/html/client.php [line:protected] => 15 [trace:Exception:private] => Array ( [0] => Array ( [file] => /var/www/html/client.php [line] => 15 [function] => __soapCall [class] => SoapClient [type] => -> [args] => Array ( [0] => core_course_get_contents [1] => Array ( [0] => 5 ) ) ) ) [previous:Exception:private] => [faultstring] => Invalid parameter value detected | ERRORCODE: invalidparameter [faultcode] => Receiver [faultactor] => invalidparameter [detail] => options => Invalid parameter value detected: Only arrays accepted. The bad value is: '' )
Web 服务的文档是:
REST (POST parameters)
courseid= int
XML-RPC (PHP structure)
[options] =>
Array
(
[0] =>
Array
(
[name] => string
[value] => string
)
)
根据代码:https://github.com/moodle/moodle/blob/ae82333cf25219ba627538f7e8de72f0b4028460/course/externallib.php#L49
预期的参数是:
- 课程编号
- 选项(可选(
因此,我希望调用看起来像这样:
$resp = $client->__soapCall($functionname, array('courseid' => $cid));
如果要指定任何选项,它将如下所示:
$resp = $client->__soapCall($functionname, array('courseid' => $cid, 'options' => [['name' => 'nameofoption', 'value' => 'valuetoset'], ['name' => 'secondoption', 'value' => 'secondvalue]]));