调用moodle webservice函数时如何在rest客户端给出参数?

时间:2015-07-30 15:23:25

标签: web-services rest

休息client.php

<?php

$token = '4f920ddef0bd90d84ef316621fde6d22';

$domainname = 'localhost';

$functionname = 'local_wstemplate_hello_world';

$we='hello';

$restformat = 'json'; 

$params = array($we);

/// REST CALL
header('Content-Type: text/plain');

$serverurl = $domainname . '/moodle/server/moodle/webservice/rest/server.php'. '?wstoken=' . $token . '&wsfunction='.$functionname;

require_once('./curl.php');

$curl = new curl;

$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"}

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

1 个答案:

答案 0 :(得分:-1)

当您使用所有必需信息而不是简单数组创建复杂对象时,它很可能会起作用。所以尝试这样的事情:

/// 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);