我正在编写一个简单的实时搜索块插件。我通过 API 访问外部网站的资源,并显示与用户在键入时搜索的内容相匹配的结果。我正计划编写一个可以执行此操作的 Web 服务。我将用户输入作为 ajax.call 中的参数之一传递,然后我的 Web 服务函数将返回建议的结果。我想知道是否有必要使用网络服务,因为我没有从 Moodle 数据库检索或返回任何数据,我不想存储建议而只显示它们。
现在我正在使用 XMLHttpRequest 在我的插件中调用一个内部 php 文件,该文件通过 api 连接并返回结果,但我想知道是否有推荐的方法来做到这一点。
//the ajax call
ajax.call([{
methodname: 'block_xxxx_loadpages',
args: {userinput: userinput},}])
// the webservice function
class block_xxxx_external extends external_api {
//parameters
public static function loadpages() {
return new external_function_parameters (
array('userinput' => new external_value(PARAM_TEXT, 'the user input'))
);
}
//the function
public static function loadpages($userinput = 'userinput') {
//parameter validation
$params = self::validate_parameters(self::hello_world_parameters(),
array('userinput' => $userinput));
//connect to api and return the result page matching the userinput
return $result;
}
public static function loadpages_returns() {
return new external_value(PARM_TEXT, 'the result')
}
}
蛊毒传说