Home > php教程 > php手册 > PHP中soap的使用例子

PHP中soap的使用例子

WBOY
Release: 2016-06-13 10:50:34
Original
1003 people have browsed it

PHP中soap的使用例子
PHP 使用soap有两种方式。

一、用wsdl文件

服务器端。

 
{
  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文件

服务器端

 
{
  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);
}
?>

Related labels:
source:php.cn
Statement of this Website
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template