Web Service not working

Web Service not working

by Abhishek Pawar -
Number of replies: 2

Dear Experts,

Please help me 

//////////// service.php /////////

<?php

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

/**
* Web service Block Ready to Help plugin template external functions and service definitions.
*
* @package readytohelp
* @copyright 2011 Jerome Mouneyrac
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

// We defined the web service functions to install.
$functions = array(
'block_readytohelp_grievance_departments' => array(
'classname' => 'block_readytohelp_external',
'methodname' => 'grievance_departments',
'classpath' => 'blocks/readytohelp/externallib.php',
'description' => 'Return Hello World FIRSTNAME. Can change the text (Hello World) sending a new text as parameter',
'type' => 'read',
)
);

// We define the services to install as pre-build services. A pre-build service is not editable by administrator.
$services = array(
'readytohelpservice' => array(
'functions' => array ('block_readytohelp_grievance_departments'),
'restrictedusers' => 0,
'enabled'=>1,
)
);


//////////// client.php /////////

<?php
// This client for block_wstemplate 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.
//

/**
* XMLRPC client for Moodle 2 - block_readytohelp
*
* This script does not depend of any Moodle code,
* and it can be called from a browser.
*
* @authorr Jerome Mouneyrac
*/

/// MOODLE ADMINISTRATION SETUP STEPS
// 1- Install the plugin
// 2- Enable web service advance feature (Admin > Advanced features)
// 3- Enable XMLRPC protocol (Admin > Plugins > Web services > Manage protocols)
// 4- Create a token for a specific user and for the service 'My service' (Admin > Plugins > Web services > Manage tokens)
// 5- Run this script directly from your browser: you should see 'Hello, FIRSTNAME'

/// SETUP - NEED TO BE CHANGED

$token = '7c4c7b1ef7dd978c89f0db0162d40581';
$restformat = 'xml'; //Also possible in Moodle 2.2 and later: 'json'
//Setting it to 'json' will fail all calls on earlier Moodle version

/// FUNCTION NAME
$functionname = 'block_readytohelp_grievance_departments';

/// PARAMETERS
$welcomemsg = 'Hello, ';

///// REST CALL
header('Content-Type: text/plain');
$serverurl = $domainname . '/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, $welcomemsg);
print_r($resp);

//////////// externallib.php /////////

<?php
// 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/>;.
/**
* External Web Service Template
*
* @package localreadytohelp
* @copyright 2011 Moodle Pty Ltd (http://moodle.com)
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require_once($CFG->libdir . "/externallib.php");

class block_readytohelp_external extends external_api {
/**
* Returns description of method parameters
* @return external_function_parameters
*/
public static function grievance_departments_parameters() {
return new external_function_parameters(
array('welcomemessage' => new external_value(PARAM_TEXT, 'The welcome message. By default it is "Hello world,"', VALUE_DEFAULT, 'Hello world, '))
);
}
/**
* Returns welcome message
* @return string welcome message
*/
public static function grievance_departments($welcomemessage = 'Hello world, ') {
global $USER;
//Parameter validation
//REQUIRED
$params = self::validate_parameters(self::grievance_departments_parameters(),
array('welcomemessage' => $welcomemessage));
//Context validation
//OPTIONAL but in most web service it should present
$context = get_context_instance(CONTEXT_USER, $USER->id);
self::validate_context($context);
//Capability checking
//OPTIONAL but in most web service it should present
if (!has_capability('moodle/user:viewdetails', $context)) {
throw new moodle_exception('cannotviewprofile');
}
return $params['welcomemessage'] . $USER->firstname ;;
}
/**
* Returns description of method result value
* @return external_description
*/
public static function grievance_departments_returns() {
return new external_value(PARAM_TEXT, 'The welcome message + user first name');
}
}

And I see this kind of error


<?xml version="1.0" encoding="UTF-8" ?>
<EXCEPTION class="dml_missing_record_exception">
<ERRORCODE>invalidrecord</ERRORCODE>
<MESSAGE>Can not find data record in database table external_functions.</MESSAGE>
<DEBUGINFO>SELECT * FROM {external_functions} WHERE name = ?
[array (
  0 =&gt; 'block_readytohelp_grievance_departments',
)]</DEBUGINFO>
</EXCEPTION>

Average of ratings: -
In reply to Abhishek Pawar

Re: Web Service not working

by Davo Smith -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

Did you increase the version number of your plugin, so that Moodle will read the service.php file and install your new webservice?

In reply to Davo Smith

Re: Web Service not working

by Abhishek Pawar -

Thank you so much Davo sir.

I updated version.php but the result come same.

this function block_readytohelp_grievance_departments is not updated in mdl_external_functions that why  this error is come


<?xml version="1.0" encoding="UTF-8" ?>
<EXCEPTION class="dml_missing_record_exception">
<ERRORCODE>invalidrecord</ERRORCODE>
<MESSAGE>Can not find data record in database table external_functions.</MESSAGE>
<DEBUGINFO>SELECT * FROM {external_functions} WHERE name = ?
[array (
  0 =&gt; 'block_readytohelp_grievance_departments',
)]</DEBUGINFO>
</EXCEPTION>