2014-01-22 166 views
1

我是Yii框架的新手,所以在yii中需要一些幫助來重寫我的php/soap web服務。 我發現Yii爲此提供了CWebService。如何在yii中創建web服務?

我創建SOAP服務器就像在此之前:

$server = new SoapServer("WebServiceWSDL.wsdl"); 

$server->addFunction('myfunction'); 

$server->handle(); 

function myfunction(){ ... 
} 

我如何使用CWebService? 簡單示例的一些幫助對於開始很有幫助。

回答

0

嘗試這樣的事情

 class WebServicesForUserController extends Controller 
     { 

      public function actions() 
      { 
       // here you need to mention the class for the web service 
       return array(
        'services'=>array(
         'class'=> 'CWebServiceAction', 
        ) 
       ); 
      } 
     // these comments are used by Yii to create wsdl for you. The following functions is a webservice 
       /** 
       * 
       * @return datetime this is the current timestamp of the server 
       * @soap 
       */ 
       public function giveTimestamp() 
       { 
        $query='select current_timestamp();'; 
        $record= Yii::app()->db->createCommand($query)->queryScalar(); 
        return $record; 
       } 

    // this action is created to consume the webservice. 
If you will visit this action then you will get the timestamp which is sent to you as a webservice response 

      public function actionLets() 
       { 
        ini_set ('soap.wsdl_cache_enable' , 0); ini_set ('soap.wsdl_cache_ttl' , 0); 
        $wsdlURL='http://localhost/project/webServicesForUser/services'; 
        $client=new SoapClient($wsdlURL); 
        $result=$client->giveTimestamp(); 
        echo $result; 
       } 

     } 

注: - 如果你想看到的WSDL只是去到你的瀏覽器,用於創建SOAP對象的URL

+1

如何使用CWebService與我自己的wsdl?你能舉個例子嗎? – Jamol