Code snippets for symfony 1.x


Snippets tagged "soap"

Soap server

This has been tested with the version ( 1.0.0beta4 ), I had a bit different structure for older version.

Ok ... here we go... how to create a soap server and take advatage of symfony's models and actions.

First we need the soap environment file, this will be the file that will initiate the soap API environment and the soap server:

name: /web/soapapi.php (soap server)

define('SF_ROOT_DIR',    realpath(dirname(__FILE__).'/..'));
define('SF_APP',         'fo');
define('SF_ENVIRONMENT', 'soap');
define('SF_DEBUG',       true);
ini_set("soap.wsdl_cache_enabled", "0");
$server = new SoapServer(''); //this file is defined in the next step
$server->setClass("mySoapController"); // more to come on this one

Now we need a wsdl file that will take advantage of the environment above.

name: /web/soapapi.wsdl (soap server definition)

<?xml version='1.0' encoding='UTF-8'?>
<definitions name="soapapi" targetNamespace="urn:soapapi" xmlns:typens="urn:soapapi" xmlns:xsd="" xmlns:soap="" xmlns:soapenc="" xmlns:wsdl="" xmlns="">
   <message name="getFactorial">
      <part name="user" type="xsd:string">user name</part>
      <part name="password" type="xsd:string">user password</part>
      <part name="number" type="xsd:float">user password</part>
   <message name="getFactorialResponse">
      <part name="return" type="xsd:float"/>
   <portType name="soapapiPortType">
      <operation name="getFactorial">
            user            required
            password            required
            Generates the factorial of a passed number.
         <input message="typens:getFactorial"/>
         <output message="typens:getFactorialResponse"/>
   <binding name="soapapiBinding" type="typens:soapapiPortType">
      <soap:binding style="rpc" transport=""/>
      <operation name="getFactorial">
         <soap:operation soapAction="urn:soapapiAction"/>
            <soap:body namespace="urn:soapapi" use="encoded" encodingStyle=""/>
            <soap:body namespace="urn:soapapi" use="encoded" encodingStyle=""/>
   <service name="soapapiService">
      <port name="soapapiPort" binding="typens:soapapiBinding">
         <soap:address location=""/>

Now we need to write our soap cotroller layer and that is the class that is asssigned by the soap server above.

name: /apps/fo/lib/mySoapController.class.php

class mySoapController extends sfController {
   public $request;
   public function __construct(){
       * Since we're bypassing Symfony's action dispatcher, we have to initialize manually.
      $this->context = sfContext::getInstance();
      $this->request = $this->context->getRequest();
   protected function soapAuth($domain,$password){
     try {
       $c = new Criteria();
       $c->add(UserPeer::USERNAME ,$domain);
       $c->add(UserPeer::PASSWORD ,$password);
       $check = UserPeer::doSelectOne($c);
         $user = $this->context->getUser();
         throw new Exception('Soap Authentication failed');
     }catch (Exception $e){       
       throw new SoapFault("1",$e->getMessage());
   function getFactorial($user, $password, $number){
     $this->soapAuth($user, $password); //I call this at the start of each function call in the soap controller (You can choose not to do it)
     $action = $this->getAction('soapapi','getFactorial');
     $result = $action->executeGetFactorial();
     return $result;

We are almost there ... Next we need action and view

name: /apps/fo/modules/soapapi/actions.class.php

class soapapiActions extends sfActions
  public function __construct(){
    if(SF_ENVIRONMENT == 'soap') $this->initialize(sfContext::getInstance());
  public function executeIndex()
    $this->forward('default', 'module');
  public function executeGetFactorial(){
    $num = $this->getRequestParameter('number');
    $result = 1;
    for($i=1;$i<$num;$i++) $result = $result + ($result * $i);
    if(SF_ENVIRONMENT == 'soap') return $result; //you can return the view or just a number... usually for soap you want to return simple stuff ... so you need to do the check
    $this->result = $result;

finally the view...

name: /apps/fo/modules/soapapi/getFactorialSuccess.php

echo "The factorial for ".$sf_params->get('number')." is {$result} ";
by Fuad Arafa on 2007-01-18, tagged soap