SMSM
SMSM

Reputation: 1519

cakephp webservice

I have problem in creating web-service using cakephp . this what i do to create this web-service . I use NuSOAP - Web Services Toolkit for PHP for this. I create a controller called WsController and import the library on it.

class WsController extends AppController{

var $uses = array();

function info()  {
    $this->layout= null;
    $ns="http://www.techvoicellc.com/Tutorials//"; 
    $server = new soap_server(); 


    $server->configureWSDL('mostafa',$ns); 
    $server->wsdl->schemaTargetNamespace=$ns; 
    $server->wsdl->addComplexType('ArrayOfstring','complexType', 
    'array','','SOAP-ENC:Array',array()
    ,array(array('ref'=>'SOAP-ENC:arrayType','wsdl:arrayType'=>'string[]')), 
    'xsd:string'); 


    $server->register('sum', 
    array('x' => 'xsd:integer','y' => 'xsd:integer'),        
    array('z' => 'xsd:integer'),   
    $ns,                                           
    "$ns#sum",                               
    'rpc',                                        
    'encoded',                                    
    'documentation'     // documentation 
    );  

    $server->service($HTTP_RAW_POST_DATA); 
}

 function sum($x,$y){ 
    $z=$x+$y;
    return new soapval('return','xsd:integer',$z);
 } 

}

and i create the clint in controller action like this

function index() {
   $wsdl = 'http://localhost/asd/ws/info?wsdl';
   $client = new nusoap_client ( $wsdl, true );

   $this->client = new nusoap_client($wsdl, true);
   $param1 = array ('x' => 2, 'y' => 1 );
   $a = $client->call ( 'sum', $param1 );
    echo $a;
 }

it don't do any thins although that i create this in non cake project and its work very well

hope some one tell me what is the best practise to create web-service in cake php

Upvotes: 0

Views: 5075

Answers (2)

Praveen Kumar M
Praveen Kumar M

Reputation: 104

This is quite Easy to develop web services in CakePHP. I have done it several times. Check the below steps.

class MyWebServicesController  extends AppController {
    var $name = 'MyWebServices';
    var $layout = "ajax";

    function index() {
        $server = new SoapServer(null);
        $server->setObject($this);
        $server->handle();
        exit(0);
    }
    public function addNumbers($a,$b) {
        return $a+$b
    }
}

Now your web service is hosted at http://webroot/MyWebServices Now you can call addNumbers like below.

$client = new SoapClient(null, array('location' => "http://webroot/MyWebServices");
$sum = $client->addNumbers(1+2);

Upvotes: 2

Ivelin
Ivelin

Reputation: 13289

It is best to create restful web service. CakePHP has everything built in for REST. All you have to do is enable it and create json/xml views.

Here is a link with your starting point: http://book.cakephp.org/2.0/en/development/rest.html

Is there a reason you want SOAP web service?

  1. It will be so much harder to create and test SOAP web service
  2. SOAP will require external libraries
  3. It will be harder for users to use the SOAP web service

Upvotes: 0

Related Questions