Turning Joomla into a SOAP Server using components

So I am using SugarCRM to feed data into Joomla. I am using SOAP for this purpose but I couldn’t really find any good tutorials on how to set up a soap server in Joomla so I am going to share my implementation here. by the way this is for joomla 2.5 i am sure it is as easy in 3.0 but I haven’t jumped ship yet.

It is actually pretty simple. I will assume you are familiar with creating components for Joomla and that you know SOAP. All you really need is a controller and a separate file with your class that you wish to bind to your SOAP Server.

this is what your controller is going to look like

<?php  
// No direct access 
defined( '_JEXEC' ) or die( 'Restricted access' );  
jimport('joomla.application.component.controller'); 
class YourcomponentControllerSugarsoapserver extends YourcomponentController { 	
  function __construct() { 		
      parent::__construct(); 
      $this->registerTask('servercall','servercall');
  }
  function servercall(){
      include_once(JPATH_SITE . '/components/com_yourcomponent/controllers/class.sugarfunctions.soap.php');
      try{
        $uri = 'http://yourdomain.net/';
        $server = new SoapServer(null,array('uri'=>$uri));
        $server->setClass('SugarFunctions');
        $server->handle();

      }catch(SoapFault $f){
        // Handle the error
      }
      $app = JFactory::GetApplication();
      $app->close();
  }
}

now you just need to feed your soap client the following URL and that’s it!
Joomla is now a soap server.

http://yourdomain.net/index.php?option=com_YOURCOMPONENT&controller=sugarsoapserver&task=servercall

Of course don’t forget to include the next to your class to protect it from intruders

defined( '_JEXEC' ) or die( 'Restricted access' );

Happy Coding

14 Comments


  1. Hi,

    I’m sorry … I know it’s a strange question and probably you can’t answer me or don’t know the component or don’t want to answer me. Perfectly understandable! 🙂
    But this is the only article I could find on the matter (Thank you by the way) and I’m at a loss.
    Can I apply this to the projectfork component?
    Where should I put the controller file?

    Thank you and sorry to disturb you!

    Reply

    1. I am not aware of this project but this can basically be done to any component. I use it in our internal one. All you need, is to add a controller to your component in the site part not the admin.

      Reply

      1. Ok Thank you!
        I try to ask you a few more questions…

        In components/com_projectfork/ there are only:
        – views (folder)
        – projectfork.php
        – router.php
        the link to enter the page of the component in the site is:
        index.php?option=com_projectfork&view=projectfork

        1) So I don’t think i have a controller … what extends the soapserver class?

        2) I have a wsdl file for my soap server… do I need to change just this:

        $uri = ‘http://yourdomain.net/’; //delete this line and
        $server = new SoapServer(‘wsdlfile.wsdl’); //change like this?

        3) In the end are the files I need for the server to work:
        – file wsdl (site part folder: components/com_projectfork/)
        – controller.php (site part folder: components/com_projectfork/)

        – class.sugarfunctions.soap.php (site part folder: components/com_projectfork/controllers/)
        ?

        4) The client sends me username and password with a soap request.. I’m able to retrieve them and I need to send back info like datetime, timeout ecc.
        Do you have some link to documentation that can help me?

        Thank you very much again

        Reply

        1. You can just make a new folder and call it controllers under your component root. make a file called mysoap.php in there copy my code and change the following line from

          class YourcomponentControllerSugarsoapserver extends YourcomponentController {

          to

          class ProjectforkControllerMysoap extends ProjectforkController {

          I didn’t use a wsdl file since this is not required by php’s SoapClient. So i can’t advice you on that.

          at the end you should have

          omponents/com_projectfork/controllers/mysoap.php

          omponents/com_projectfork/controllers/mysoapclassfile.php in my case this is class.sugarfunctions.soap.php but it can be any file with your class.

          wsdl i am not sure.

          Reply

  2. Sorry,
    another question…

    the client that sends a request to my soap server doesn’t have to login?
    How does it works?

    Thank you

    Reply

    1. This component is open for access, the login is handled by your soap requests. This all depends how you have implemented your client-server.there are many ways to do this. you could have a login request that authenticates you in the joomla site, then the joomla site can give you back a session ID or a login-token, you use this token/sessionID on your other requests. we use community builder so we let that module handle the login, since for us any request contains all the information it needs we pass a joomla user name and password with the request to community builder. I might be making this sound complicated but the point i want to drive across is that the authentication is up to your SOAP implementation

      Reply

  3. According to your instrunctions, I get the message “restricted access”, which seems logic to me. Because its a Joomla security feature, to prevent external access to Joomla information.

    How do you work around the restricted access message, with your soap access?

    Reply

    1. Hi sorry but i didn’t see this message earlier. If you are connecting your client using http://yourdomain.net/index.php?option=com_YOURCOMPONENT&controller=sugarsoapserver&task=servercall then you are going through joomla and _JEXEC is defined in which case you should not get that error message. joomla would only restrict access to links in their menus that are not public. since you are accessing the module directly there is no access check being done by the cms, it is up to the component to restrict access

      Reply

  4. Hi,
    even if this post is old I hope you still check it and you can help me …

    If I register one more task in the controller of your example like this:

    $this->registerTask(‘servercalltwo’,’servercall’);

    it should call the same method than the one you created. Then I should be able to retrieve the task “called” (servercall or servercalltwo) with getTask().
    My question is this: How can I pass to or retrieve the task called inside the functions/methods inside the class SugarFunctions so that I can use the task to differentiate how the soap request is handled based on the task called?

    I need to differentiate slightly the functions of the class and I’d like to do it without doubling all the files class etc. and in the end without creating two distinct webservices.

    Is it possible? If yes can you help me to understand how? Or do you have a link that can help me?

    Thank you, I really hope you read this and help me!
    Mic

    Reply

    1. Hi again,
      If I get the task and save it in a variable and pass it in the soapserver array as you did with $uri can I retrieve it inside the SugarFunctions class functions and use it? How can I do it if it is possible? Would the requests handled correctly based on the task passed?

      Do I retrieve it with func_get_args()more or less like in this example?
      http://php.net/manual/en/function.func-get-args.php
      Or in another way?

      Thank you again
      mic

      Reply

      1. Humm… You can check the $_POST or $_GET Global variables. The task name should be in one of them

        Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.