首页 >后端开发 >php教程 >PHP企业级应用之WebService篇_PHP

PHP企业级应用之WebService篇_PHP

WBOY
WBOY原创
2016-06-01 12:25:13904浏览

来段企业级应用吧,主要是讲PHP5对webservice的一些实现(以下的程序可以被JAVA,NET,C等正常调用)

国内用PHP写WebService的真的很少,网上资料也没多少,公司的项目开发过程中,经历了不少这方面的东西,写出来以供大家参考(谢谢老农提供的WSDL和程序文件)

客户端

header ( "Content-Type: text/html; charset=utf-8" );
/*
* 指定WebService路径并初始化一个WebService客户端
*/
$ws = "http://soap/soapCspMessage.php?wsdl";
$client = new SoapClient ( $ws, array ('trace' => 1, 'uri' => 'http://www.zxsv.com/SoapDiscovery/' ) );
/*
* 获取SoapClient对象引用的服务所提供的所有方法
*/
echo ("SOAP服务器提供的开放函数:");
echo ('

');<br>var_dump ( $client->__getFunctions () );<br>echo ('
');
echo ("SOAP服务器提供的Type:");
echo ('
');<br>var_dump ( $client->__getTypes () );<br>echo ('
');
echo ("执行GetGUIDNode的结果:");
//$users = $client->GetUsers();
//var_dump($HelloWorld );
$parameters = array('uname'=>'zxsv',"upassword"=>'123');
    $out = $client->HelloWorld($parameters);
    $datadb = $out->HelloWorldResponse;
    var_dump($out);
?>

服务端

class Member
{
    public $UserId;
    public $Name;
    public function __construct($parmas){
        $this->UserId = $parmas[0];
        $this->Name = $parmas[1];
    }
}
$servidorSoap = new SoapServer('testphp.xml',array('uri' => 'http://www.TestPHP.com/','encoding'=>'utf-8','soap_version' => SOAP_1_2 ));
$servidorSoap->setClass(Testphp);
$servidorSoap->handle();
class Testphp {
    public function HelloWorld($uid){
        return array('HelloWorldResult'=>"mystring".$uid->{'uname'}.' and '.$uid->{'upassword'});
    }
    public function GetMember($uid){
        $s=array();
        for($i=0;$i{'uid'};$i++){
            $s[] =&new Member(array($i, $uid->{'uname'}.'我测试'.$i)); 
        }
        return   array('GetMemberResult'=>$s);
    }
}
?>

到这里应该都看的懂吧
下面是WSDL文件


http://schemas.xmlsoap.org/wsdl/soap/
" xmlns:tm="http://microsoft.com/wsdl/mime/textMatching/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/" xmlns:tns="http://www.TestPHP.com/" xmlns:s="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" targetNamespace="http://www.TestPHP.com/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">
 
    http://www.TestPHP.com/
">
     
       
         
           
           
         

       

     

     
       
         
           
         

       

     

     
       
         
           
           
         

       

     

     
       
         
           
         

       

     

     
       
         
       

     

     
       
         
         
       

     

   
 
 
   
 

 
   
 

 
   
 

 
   
 

 
   
     
     
   

   
     
     
   

 

 
    http://schemas.xmlsoap.org/soap/http
" />
   
      http://www.TestPHP.com/HelloWorld
"   />
     
     
       
     

   
   
      http://www.TestPHP.com/GetMember
"  />
     
     
       
     

   
 
 
    http://schemas.xmlsoap.org/soap/http
" />
   
      http://www.TestPHP.com/HelloWorld
"  />
     
     
       
     

   
   
      http://www.TestPHP.com/GetMember
"  />
     
     
       
     

   
 
 
   
     

http://soap/goodwsdl/testphp.php
" />
   
   
     
http://soap/goodwsdl/testphp.php
" />
   
 

 

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn