PHP SOAP服务示例代码

 

服务端:

<?php
class Default_Model_Soap_Soap {

//客户端需要调用的函数

    public function getsum($a, $b)
    {
        if (! Zend_Validate::is ( $a, 'Int' )) {
            return '';
        }
        if (! Zend_Validate::is ( $b, 'Int' )) {
            return '';
        }
        
        return $a + $b;
    }
}


 

//声明服务

 

<?php
class ServerController extends Default_Controller_BaseAction {

    public function serverAction()
    {

       //这里声明服务的soap访问地址
        $server = new Zend_Soap_Server ( null, array ('uri' => 'http://www.jbqq.com/default/server/server' ) );
        $server->setClass ( 'Default_Model_Soap_Soap' );
        $server->handle ();
        exit();
    }

}


//客户端

<?php
class SoapController extends  Default_Controller_BaseAction {
    
    public function indexAction()
    {

      //填写soap服务的访问地址
        $options = array ('location' =>  'http://www.jbqq.com/default/server/server', 'uri' => 'http://www.jbqq.com/default/server/server' );
        try {
            $client = new Zend_Soap_Client ( null, $options );
            
            $result = $client->getsum ( 108, 56 );   //调用服务端声明的soap服务类中的函数
            if ($result != '') {
                echo $result;
            } else {
                echo 'err';
            }
        } catch ( SoapFault $s ) {
            echo $s->faultcode . '->' . $s->faultstring;
        } catch ( Exception $e ) {
            echo $e->getMessage ();
        }
        exit ();
    }
    
}


 

你可能感兴趣的:(PHP,exception,server,function,SOAP,Zend)