Calling Server Function in WebService external Function.

Calling Server Function in WebService external Function.

by Syed Nayab Bukhari -
Number of replies: 3
Picture of Core developers

Dear Seniors

I developed a script for Bulk Course Creation via coursecreate() function. 

coursecreate0 get data from a webservice one the base of unique id. 

Each id has contents of whole course. 

createcourse() 

Now I am going to develop a webservice, So on call of that web service, coursecreate() should called and return course link to client via protocol as done in coursecreate() function(locally). 

Can any one tell me how I can call coursecreate() in webservice external function. 

I tried in different ways

1.  include whole page having coursecreate()   // lib file path error

2.  Add whole createcourse() in external function of web service // Error nothing showing Null

string(180) "<?xml version="1.0" encoding="UTF-8"?>
<methodCall>
<methodName>createcourse</methodName>
<params>
 <param>
  <value>
   <int>17</int>
  </value>
 </param>
</params>
</methodCall>
"

I think in am not passing params in proper way to createcourse();

 

$params = array('iid' => new external_value(PARAM_INT, 'RA id. By default it is "New Course ID,"', VALUE_DEFAULT, '17'))

 

public static function createcourse(int $iid){....

//also tried

public static function createcourse($iid){..

//Return params

return new external_value(PARAM_TEXT, 'The Course link');

 

Please help me So, It returns courselink as it is working on local server.

 

Waiting for seniors reply. 

Attachment WebService NULL Return Error.JPG
Average of ratings: -
In reply to Syed Nayab Bukhari

Re: Calling Server Function in WebService external Function.

by Syed Nayab Bukhari -
Picture of Core developers

Please anyone suggest me 

What is I am doing wrong. 

In reply to Syed Nayab Bukhari

Re: Calling Server Function in WebService external Function.

by Syed Nayab Bukhari -
Picture of Core developers

With great blessing of Almighty Allah. I completed this task.

1.  allow single url from php.ini        set on  

2.  include required page having function you want to include in service external function. 

3.  add function in web service external function and

web_external_fun(){

      return functionname();

}

It is working fine .. Now I am testing my Web Service on another Server. Let c

Attachment newly created coruse via web service.JPG
In reply to Syed Nayab Bukhari

Re: Calling Server Function in WebService external Function.

by Syed Nayab Bukhari -
Picture of Core developers

Please tell me what I am doing Wrong. 

When I call WebService 

http://localhost/bpms/webservice/rest/server.php?wstoken=4e32dfedce5436511a97478298d00705&wsfunction=local_createcourse&moodlewsrestformat=json&iid=113

local_createcourse  // externallib function. 

It shows error 

{"exception":"dml_missing_record_exception","errorcode":"invalidrecord","message":"Can not find data record in database table external_functions.","debuginfo":"SELECT * FROM {external_functions} WHERE name = ?\n[array (\n  0 => 'local_createcourse',\n)]"}

 

http://localhost/bpms/webservice/rest/server.php?wstoken=4e32dfedce5436511a97478298d00705&wsfunction=local_createcourses&moodlewsrestformat=json&iid=113

local_createcourses  // Web Service function mentioned in services.php

It shows error 

The website encountered an error while retrievinghttp://localhost/bpms/webservice/rest/server.php?wstoken=4e32dfedce5436511a97478298d00705&wsfunction=local_createcourses&moodlewsrestformat=json&iid=113. It may be down for maintenance or configured incorrectly.

 

Details of all page (client.php, services.php, externallib.php) are attached.  

[code]

What is I am doing wrong Please help me

/////////////////// db->Service Page

$functions = array(

        'local_createcourses' => array(

                'classname'   => 'createcourse_external',       // class name

                'methodname'  => 'local_createcourse',      // class main function

                'classpath'   => 'local/createcourse/externallib.php',  // plugin path

                'description' => 'Return course link',      // description

                'type'        => 'write',                   // mode write or read

        )

);

 

// We define the services to install as pre-build services. A pre-build service is not editable by administrator.

$services = array(

        'Risk Assessment Tool' => array(

                        'functions' => array ('local_createcourses'),   // client function call name, both will same this and function array

                        'restrictedusers' => 0,

                        'enabled'=>1,

            )

);

 

/////////////////// external Page

require_once('/createcourse.php'); // local custom function work perfectly independently

class createcourse_external extends external_api {

    /**

     * @Returns description of method parameters

     * @return external_function_parameters

     */   

    public static function createcourse_parameters(){

        return new external_function_parameters(

                array('iid' => new external_value(PARAM_INT, 'iid'))

        );

    }

    /**

     * @Returns ID

     * @return string course link

     */

    public static function local_createcourse($iid){

        $url=creates($iid);

        return $url;

    }   

    /**

     * @Returns description of method result value

     * @return external_description

     */

       

       public static function createcourse_returns() {

        return new external_value(PARAM_TEXT, 'The Course link');

    }  

}

 

/////////Client Page

include('../externallib.php');

/// SETUP - NEED TO BE CHANGED

$token = '4e32dfedce5436511a***********';

$domainname = 'http://localhost/bpms';

 

/// FUNCTION NAME

//$functionname = 'local_createcourses';  // as define in services function name

 

/// PARAMETERS

//$iid=$_GET[‘iid’];

 

///// XML-RPC CALL

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

$serverurl = $domainname . '/webservice/rest/server.php'. '?wstoken=' . $token;

 

require_once('/curl.php');

$curl = new curls;

              

 $post = xmlrpc_encode_request('local_createcourses',local_createcourse($iid), array('encoding' => 'UTF-8'));

 

$resp = xmlrpc_decode($curl->post($serverurl, $post));

print_r($resp);

return $resp;

    }

}

 

[/code]

Attachment external function table.JPG