Home  >  Article  >  Backend Development  >  PHP中soap的用法实例_PHP

PHP中soap的用法实例_PHP

WBOY
WBOYOriginal
2016-05-31 19:29:02822browse

本文实例讲述了PHP中soap的用法,分享给大家供大家参考。具体用法分析如下:

PHP 使用soap有两种方式。

一、用wsdl文件

服务器端:

代码如下:

class service
{
  public function HelloWorld()
   {
      return  "Hello";
   }
  public  function Add($a,$b)
   {
      return $a+$b;
   }
}
$server=new SoapServer('soap.wsdl',array('soap_version' => SOAP_1_2));
$server->setClass("service");
$server->handle();
?>


资源描述文件,可以用工具(zend studio)生成。其实就是一个xml文件。

代码如下:



 
   
     
       
         
           
         

       

     

     
       
         
           
         

       

     

     
       
        
         
        

       

     

     
       
        

         
        
       
     
   
 
       
   
 

 
   
 

      
     
     
   

 

 
        transport="http://schemas.xmlsoap.org/soap/http" />
   
    
    
    
            namespace="http://localhost/interface/" />
    

   

 

 
   
     


   
 


客户端调用:

代码如下:

$soap = new SoapClient('http://localhost/interface/soap.wsdl');
echo $soap->Add(1,2);
?>


二、不用wsdl文件

服务器端:

代码如下:

class service
{
  public function HelloWorld()
   {
      return  "Hello";
   }
  public  function Add($a,$b)
   {
      return $a+$b;
   }
}
$server=new SoapServer(null,array('uri' => "abcd"));
$server->setClass("service");
$server->handle();
?>


客户端:

代码如下:

try{
 $soap = new SoapClient(null,array(
   "location" => "http://localhost/interface/soap.php",
   "uri"      => "abcd",  //资源描述符服务器和客户端必须对应
   "style"    => SOAP_RPC,
   "use"      => SOAP_ENCODED
      ));

 echo $soap->Add(1,2);
}catch(Exction $e){
 echo print_r($e->getMessage(),true);
}
?>

希望本文所述对大家的PHP程序设计有所帮助。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn