How To / General

Adding custom web service functions

  • saeid
  • Topic Author
  • Offline
  • Junior Member
  • Junior Member
More
3 years 2 months ago #1 by saeid
Hi,

I downloaded and installed this plugin:
www.joomdle.com/download/category/35-dev...le-web-services-demo

Then I added the testjoomdlewsdemo function to Joomdle external services.



But when I call the function in Joomla by this code:
Code:
require_once 'administrator/components/com_joomdle/helpers/content.php'; $return = JoomdleHelperContent::call_method ('testjoomdlewsdemo', "TEST STRING"); print_r($return);

It gives this error:
ERROR: Joomdle external signature not found: testjoomdlewsdemo

Please help.

Please Log in or Create an account to join the conversation.

More
3 years 2 months ago #2 by Antonio Durán
Replied by Antonio Durán on topic Adding custom web service functions
Hi.

I'll need to update this example plugin to include support for REST, as it is outdated now.

Please Log in or Create an account to join the conversation.

  • saeid
  • Topic Author
  • Offline
  • Junior Member
  • Junior Member
More
3 years 2 months ago #3 by saeid
Replied by saeid on topic Adding custom web service functions
Hi, Did you update the plugin?

Please Log in or Create an account to join the conversation.

More
3 years 2 months ago #4 by Antonio Durán
Replied by Antonio Durán on topic Adding custom web service functions
Sorry for the delay. It was not the plugin that needs updating, I need to modify Joomdle core to allow for direct REST web service calls. I will do so for next release.

If you want, you can add the code yourself.
File: administrator/components/com_joomdle/helpers/content.php
Code:
static function call_method_rest_named_params ($method, $params) { $cm = JoomdleHelperContent::_get_cm (); if ($cm == 'fgc') $response = JoomdleHelperContent::call_method_rest_fgc_named_params ($method, $params); else $response = JoomdleHelperContent::call_method_rest_curl_named_params ($method, $params); return $response; } static function call_method_rest_curl_named_params ($method, $params) { $moodle_rest_server_url = JoomdleHelperContent::_get_rest_url (); $url = $moodle_rest_server_url . '&wsfunction=joomdle_' . $method; $request = JoomdleHelperContent::format_postdata_for_curlcall($params); $headers = array(); array_push($headers,"Content-Type: application/x-www-form-urlencoded"); array_push($headers,"Content-Length: ".strlen($request)); array_push($headers,"\r\n"); $ch = curl_init(); curl_setopt( $ch, CURLOPT_URL, $url); # URL to post to curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 ); # return into a variable curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers ); # custom headers, see above curl_setopt( $ch, CURLOPT_POSTFIELDS, $request ); curl_setopt( $ch, CURLOPT_CUSTOMREQUEST, 'POST' ); # This POST is special, and uses its specified Content-type $response = curl_exec( $ch ); # run! curl_close($ch); $response = trim ($response); $response = json_decode ($response, true); return $response; } static function call_method_rest_fgc_named_params ($method, $params) { $moodle_rest_server_url = JoomdleHelperContent::_get_rest_url (); $url = $moodle_rest_server_url . '&wsfunction=joomdle_' . $method; $request = JoomdleHelperContent::format_postdata_for_curlcall($params); $context = stream_context_create(array('http' => array( 'method' => "POST", 'header' => "Content-Type: application/x-www-form-urlencoded", 'content' => $request ))); $file = @file_get_contents($url , false, $context); $file = trim ($file); $response = json_decode ($file, true); return $response; }

Then, you can call the method like this:
Code:
$a = JoomdleHelperContent::call_method_rest_named_params ('testjoomdlewsdemo', array ('teststring' => "TEST STRING")); echo "<pre>"; print_R ($a);

Please Log in or Create an account to join the conversation.

  • saeid
  • Topic Author
  • Offline
  • Junior Member
  • Junior Member
More
3 years 2 months ago #5 by saeid
Replied by saeid on topic Adding custom web service functions
Thank you. It works correctly.

Please Log in or Create an account to join the conversation.

More
3 years 2 months ago #6 by Antonio Durán
Replied by Antonio Durán on topic Adding custom web service functions
Great, thanks for the feedback.

Please Log in or Create an account to join the conversation.