Web services 调用moodle webservice函数时,如何在rest客户端中指定参数?

Web services 调用moodle webservice函数时,如何在rest客户端中指定参数?,web-services,rest,Web Services,Rest,restclient.php 我是moodle的新手。所以任何人都可以告诉我如何将参数传递给函数?当您创建一个包含所有必需信息的复杂对象而不是一个简单数组时,它很可能会起作用。因此,请尝试以下方法: /// SETUP - NEED TO BE CHANGED $token = '4f920ddef0bd90d84ef316621fde6d22'; $domainname = 'localhost'; $functionname = 'core_user_create_users'; //

restclient.php


我是moodle的新手。所以任何人都可以告诉我如何将参数传递给函数?

当您创建一个包含所有必需信息的复杂对象而不是一个简单数组时,它很可能会起作用。因此,请尝试以下方法:

/// SETUP - NEED TO BE CHANGED
$token =  '4f920ddef0bd90d84ef316621fde6d22';
$domainname = 'localhost';
$functionname = 'core_user_create_users';

// REST RETURNED VALUES FORMAT
$restformat = 'json';

/// PARAMETERS - NEED TO BE CHANGED IF YOU CALL A DIFFERENT FUNCTION
$user1 = new stdClass();
$user1->username = 'testusername10';
$user1->password = 'Testpassword1*';
// ...more properties omitted
$preferencename1 = 'preference1';
$preferencename2 = 'preference2';
$user1->preferences = array(
    array('type' => $preferencename1, 'value' => 'preferencevalue1'),
    array('type' => $preferencename2, 'value' => 'preferencevalue2'));

$users = array($user1);
$params = array('users' => $users);

/// REST CALL
header('Content-Type: text/plain');
$serverurl = $domainname . '/webservice/rest/server.php'. '?wstoken=' . $token . '&wsfunction='.$functionname;
require_once('./curl.php');

$curl = new curl;
//if rest format == 'xml', then we do not add the param for backward compatibility with Moodle < 2.2
$restformat = ($restformat == 'json')?'&moodlewsrestformat=' . $restformat:'';
$resp = $curl->post($serverurl . $restformat, $params);
print_r($resp);
{"exception":"invalid_parameter_exception","errorcode":"invalidparameter","message":"Invalid parameter value detected","debuginfo":"Missing required key in single structure: welcomemessage"}
/// SETUP - NEED TO BE CHANGED
$token =  '4f920ddef0bd90d84ef316621fde6d22';
$domainname = 'localhost';
$functionname = 'core_user_create_users';

// REST RETURNED VALUES FORMAT
$restformat = 'json';

/// PARAMETERS - NEED TO BE CHANGED IF YOU CALL A DIFFERENT FUNCTION
$user1 = new stdClass();
$user1->username = 'testusername10';
$user1->password = 'Testpassword1*';
// ...more properties omitted
$preferencename1 = 'preference1';
$preferencename2 = 'preference2';
$user1->preferences = array(
    array('type' => $preferencename1, 'value' => 'preferencevalue1'),
    array('type' => $preferencename2, 'value' => 'preferencevalue2'));

$users = array($user1);
$params = array('users' => $users);

/// REST CALL
header('Content-Type: text/plain');
$serverurl = $domainname . '/webservice/rest/server.php'. '?wstoken=' . $token . '&wsfunction='.$functionname;
require_once('./curl.php');

$curl = new curl;
//if rest format == 'xml', then we do not add the param for backward compatibility with Moodle < 2.2
$restformat = ($restformat == 'json')?'&moodlewsrestformat=' . $restformat:'';
$resp = $curl->post($serverurl . $restformat, $params);
print_r($resp);