用php编写web service方式一之soap

1.设置php.ini

windsows系�y是extension=php_soap.dll和extension=php_openssl.dll
unix系�y是extension=php_soap.so 和extension=php_openssl.so
重��IIS或apache
2.编写soapservice.php,也就是soap服务器端
require_once './config.inc.php';
$db=mysql_connect($dbhost, $dbuser, $dbpw) or die('Can\'t connect the db_server');
mysql_select_db($dbname,$db) or die('DataBase do not exit()');
Class Service{
// function __construct(){}
function GetmemberData($strWhere=''){
   global $db, $tablepre;
   //查����T�Y料
   $GetmemberData="";
   $sqlstr="SELECT * FROM {$tablepre}members WHERE 1=1   $strWhere";
  
   $Query=mysql_query($sqlstr,$db) or die("Wrong! ".$sqlstr);
   $row= mysql_fetch_array($Query,MYSQL_ASSOC) ;
   foreach($row as $key=>$value){
    $GetmemberData.='&'.$key.'='.$value;
   }
   return $GetmemberData;  
}

      function DetectMember($UserName=''){
         //��y���T�ぬ�是否存在 
   global $db, $tablepre;
         $DetectMember = false;
         try{
             $sqlstr = "SELECT username FROM {$tablepre}members WHERE (username = '$UserName')";
    $Query=mysql_query($sqlstr,$db) or die("Wrong! ".$sqlstr);
    $row= mysql_fetch_array($Query,MYSQL_ASSOC) ;
    if($row){
     $DetectMember = true;
    }
   }
         catch(Exception $e){
             $DetectMember = false;
         }
   return $DetectMember;
     }

function ChangePassword($UserName='', $OldPassword='', $NewPassword=''){
   //更改密�a
   global $db, $tablepre;
   $ChangePassword = false;
   try{
    $sqlstr = "SELECT username FROM {$tablepre}members WHERE (username = '$UserName' AND password='".md5($OldPassword)."')";
    $Query=mysql_query($sqlstr,$db) or die("Wrong! ".$sqlstr);
    $row= mysql_fetch_array($Query,MYSQL_ASSOC) ;
    if($row){
     $sqlstr = "UPDATE {$tablepre}members SET (password='".md5($NewPassword)."') FROM   WHERE (username = '$UserName' AND password='".md5($OldPassword)."')";
     if(mssql_query($sqlstr,$db)){
      $ChangePassword = true;
     }
    }            
   }catch(Exception $e){
    $ChangePassword = false;
   }
   return $ChangePassword;
}
     function CheckMember($UserName='', $Password=''){
         //��^���T�ぬ�密�a
         $CheckMember = false;
         try{
    $sqlstr = "SELECT username FROM {$tablepre}members WHERE (username = '$UserName' AND password='".md5($OldPassword)."')";
    $Query=mysql_query($sqlstr,$db) or die("Wrong! ".$sqlstr);
    $row= mysql_fetch_array($Query,MYSQL_ASSOC) ;
             if($row){
     $CheckMember = true;
    }
         }catch(Exception $e){
             $CheckMember = false;
         }
   return $CheckMember;
     }
}
$server = new SoapServer("soap_wsdl.wsdl");  
$server->setClass("Service");  
$server->handle();[/code]3.编写wsdl,也就是web service description language (web服务描述语言),命名为soap_wsdl.wsdl[code]<?xml version='1.0' encoding='UTF-8'?>
<definitions name="sooostar_forum" targetNamespace="urn:sooostar_forum" xmlns:typens="urn:sooostar_forum" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns="http://schemas.xmlsoap.org/wsdl/">
<message name="ChangePassword">
    <part name="UserName" type='xsd:string'/>
    <part name="OldPassword" type='xsd:string'/>
    <part name="NewPassword" type='xsd:string'/>
</message>
<message name="ChangePasswordResponse">
    <part name="ChangePasswordReturn" type='xsd:boolean'/>
</message>
<message name="CheckMember">
    <part name="UserName" type='xsd:string'/>
    <part name="Password" type='xsd:string'/>
</message>
<message name="CheckMemberResponse">
    <part name="CheckMemberReturn" type='xsd:boolean'/>
</message>
<message name="DetectMember">
    <part name="UserName" type='xsd:string'/>
</message>
<message name="DetectMemberResponse">
    <part name="DetectMemberReturn" type='xsd:boolean'/>
</message>
<message name="GetErrorMessage"/>
<message name="GetErrorMessageResponse">
    <part name="GetErrorMessageReturn" type='xsd:string'/>
</message>
<message name="GetmemberData">
    <part name="UserName" type='xsd:string'/>
</message>
<message name="GetmemberDataResponse">
    <part name="GetmemberDataReturn" type='xsd:string'/>
</message>
<message name="GetMemberUserName">
    <part name="NickName" type='xsd:string'/>
</message>
<message name="GetMemberUserNameResponse">
    <part name="GetMemberUserNameReturn" type='xsd:boolean'/>
</message>
<message name="JoinMember">
    <part name="UserName" type='xsd:string'/>
    <part name="Password" type='xsd:string'/>
    <part name="NickName" type='xsd:string'/>
    <part name="Image" type='xsd:string'/>
    <part name="SubIntro" type='xsd:string'/>
    <part name="SignText" type='xsd:string'/>
    <part name="Sex" type='xsd:string'/>
    <part name="EMail" type='xsd:string'/>
    <part name="Birth_Y" type='xsd:string'/>
    <part name="Birth_M" type='xsd:string'/>
    <part name="Birth_D" type='xsd:string'/>
    <part name="Tel" type='xsd:string'/>
    <part name="Address" type='xsd:string'/>
    <part name="FromWeb" type='xsd:string'/>
</message>
<message name="JoinMemberResponse">
    <part name="JoinMemberReturn" type='xsd:boolean'/>
</message>
<message name="ModifyMember">
    <part name="UserName" type='xsd:string'/>
    <part name="NickName" type='xsd:string'/>
    <part name="Image" type='xsd:string'/>
    <part name="SubIntro" type='xsd:string'/>
    <part name="SignText" type='xsd:string'/>
    <part name="Sex" type='xsd:string'/>
    <part name="EMail" type='xsd:string'/>
    <part name="Birth_Y" type='xsd:string'/>
    <part name="Birth_M" type='xsd:string'/>
    <part name="Birth_D" type='xsd:string'/>
    <part name="Tel" type='xsd:string'/>
    <part name="Address" type='xsd:string'/>
</message>
<message name="ModifyMemberResponse">
    <part name="ModifyMemberReturn" type='xsd:boolean'/>
</message>
<message name="QueryMemberData">
    <part name="UserName" type='xsd:string'/>
    <part name="SubIntroFlag" type='xsd:boolean'/>
</message>
<message name="QueryMemberDataResponse">
    <part name="QueryMemberDataReturn" type='xsd:string'/>
</message>
<message name="QueryNickName">
    <part name="UserName" type='xsd:string'/>
</message>
<message name="QueryNickNameResponse">
    <part name="QueryNickNameReturn" type='xsd:string'/>
</message>
<portType name="ServicePortType">
    <operation name="ChangePassword">
     <input message="typens:ChangePassword"/>
     <output message="typens:ChangePasswordResponse"/>
    </operation>
    <operation name="CheckMember">
     <input message="typens:CheckMember"/>
     <output message="typens:CheckMemberResponse"/>
    </operation>
    <operation name="DetectMember">
     <input message="typens:DetectMember"/>
     <output message="typens:DetectMemberResponse"/>
    </operation>
    <operation name="GetErrorMessage">
     <input message="typens:GetErrorMessage"/>
     <output message="typens:GetErrorMessageResponse"/>
    </operation>
    <operation name="GetmemberData">
     <input message="typens:GetmemberData"/>
     <output message="typens:GetmemberDataResponse"/>
    </operation>
    <operation name="GetMemberUserName">
     <input message="typens:GetMemberUserName"/>
     <output message="typens:GetMemberUserNameResponse"/>
    </operation>
    <operation name="JoinMember">
     <input message="typens:JoinMember"/>
     <output message="typens:JoinMemberResponse"/>
    </operation>
    <operation name="ModifyMember">
     <input message="typens:ModifyMember"/>
     <output message="typens:ModifyMemberResponse"/>
    </operation>
    <operation name="QueryMemberData">
     <input message="typens:QueryMemberData"/>
     <output message="typens:QueryMemberDataResponse"/>
    </operation>
    <operation name="QueryNickName">
     <input message="typens:QueryNickName"/>
     <output message="typens:QueryNickNameResponse"/>
    </operation>
</portType>
<binding name="ServiceBinding" type="typens:ServicePortType">
    <soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/>
    <operation name="ChangePassword">
     <soap:operation soapAction="urn:ServiceAction"/>
     <input>
      <soap:body namespace="urn:sooostar_forum" use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
     </input>
     <output>
      <soap:body namespace="urn:sooostar_forum" use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
     </output>
    </operation>
    <operation name="CheckMember">
     <soap:operation soapAction="urn:ServiceAction"/>
     <input>
      <soap:body namespace="urn:sooostar_forum" use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
     </input>
     <output>
      <soap:body namespace="urn:sooostar_forum" use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
     </output>
    </operation>
    <operation name="DetectMember">
     <soap:operation soapAction="urn:ServiceAction"/>
     <input>
      <soap:body namespace="urn:sooostar_forum" use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
     </input>
     <output>
      <soap:body namespace="urn:sooostar_forum" use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
     </output>
    </operation>
    <operation name="GetErrorMessage">
     <soap:operation soapAction="urn:ServiceAction"/>
     <input>
      <soap:body namespace="urn:sooostar_forum" use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
     </input>
     <output>
      <soap:body namespace="urn:sooostar_forum" use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
     </output>
    </operation>
    <operation name="GetmemberData">
     <soap:operation soapAction="urn:ServiceAction"/>
     <input>
      <soap:body namespace="urn:sooostar_forum" use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
     </input>
     <output>
      <soap:body namespace="urn:sooostar_forum" use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
     </output>
    </operation>
    <operation name="GetMemberUserName">
     <soap:operation soapAction="urn:ServiceAction"/>
     <input>
      <soap:body namespace="urn:sooostar_forum" use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
     </input>
     <output>
      <soap:body namespace="urn:sooostar_forum" use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
     </output>
    </operation>
    <operation name="JoinMember">
     <soap:operation soapAction="urn:ServiceAction"/>
     <input>
      <soap:body namespace="urn:sooostar_forum" use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
     </input>
     <output>
      <soap:body namespace="urn:sooostar_forum" use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
     </output>
    </operation>
    <operation name="ModifyMember">
     <soap:operation soapAction="urn:ServiceAction"/>
     <input>
      <soap:body namespace="urn:sooostar_forum" use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
     </input>
     <output>
      <soap:body namespace="urn:sooostar_forum" use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
     </output>
    </operation>
    <operation name="QueryMemberData">
     <soap:operation soapAction="urn:ServiceAction"/>
     <input>
      <soap:body namespace="urn:sooostar_forum" use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
     </input>
     <output>
      <soap:body namespace="urn:sooostar_forum" use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
     </output>
    </operation>
    <operation name="QueryNickName">
     <soap:operation soapAction="urn:ServiceAction"/>
     <input>
      <soap:body namespace="urn:sooostar_forum" use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
     </input>
     <output>
      <soap:body namespace="urn:sooostar_forum" use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
     </output>
    </operation>
</binding>
<service name="sooostar_forumService">
    <port name="ServicePort" binding="typens:ServiceBinding">
     <soap:address location="http://61.62.130.46/10/glofish_ms/webservice/service.php"/>
    </port>
</service>
</definitions>
3.编写wsdl,也就是web service description language (web服务描述语言),命名为soap_wsdl.wsdl
]<?xml version='1.0' encoding='UTF-8'?>
<definitions name="sooostar_forum" targetNamespace="urn:sooostar_forum" xmlns:typens="urn:sooostar_forum" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns="http://schemas.xmlsoap.org/wsdl/">
<message name="ChangePassword">
    <part name="UserName" type='xsd:string'/>
    <part name="OldPassword" type='xsd:string'/>
    <part name="NewPassword" type='xsd:string'/>
</message>
<message name="ChangePasswordResponse">
    <part name="ChangePasswordReturn" type='xsd:boolean'/>
</message>
<message name="CheckMember">
    <part name="UserName" type='xsd:string'/>
    <part name="Password" type='xsd:string'/>
</message>
<message name="CheckMemberResponse">
    <part name="CheckMemberReturn" type='xsd:boolean'/>
</message>
<message name="DetectMember">
    <part name="UserName" type='xsd:string'/>
</message>
<message name="DetectMemberResponse">
    <part name="DetectMemberReturn" type='xsd:boolean'/>
</message>
<message name="GetErrorMessage"/>
<message name="GetErrorMessageResponse">
    <part name="GetErrorMessageReturn" type='xsd:string'/>
</message>
<message name="GetmemberData">
    <part name="UserName" type='xsd:string'/>
</message>
<message name="GetmemberDataResponse">
    <part name="GetmemberDataReturn" type='xsd:string'/>
</message>
<message name="GetMemberUserName">
    <part name="NickName" type='xsd:string'/>
</message>
<message name="GetMemberUserNameResponse">
    <part name="GetMemberUserNameReturn" type='xsd:boolean'/>
</message>
<message name="JoinMember">
    <part name="UserName" type='xsd:string'/>
    <part name="Password" type='xsd:string'/>
    <part name="NickName" type='xsd:string'/>
    <part name="Image" type='xsd:string'/>
    <part name="SubIntro" type='xsd:string'/>
    <part name="SignText" type='xsd:string'/>
    <part name="Sex" type='xsd:string'/>
    <part name="EMail" type='xsd:string'/>
    <part name="Birth_Y" type='xsd:string'/>
    <part name="Birth_M" type='xsd:string'/>
    <part name="Birth_D" type='xsd:string'/>
    <part name="Tel" type='xsd:string'/>
    <part name="Address" type='xsd:string'/>
    <part name="FromWeb" type='xsd:string'/>
</message>
<message name="JoinMemberResponse">
    <part name="JoinMemberReturn" type='xsd:boolean'/>
</message>
<message name="ModifyMember">
    <part name="UserName" type='xsd:string'/>
    <part name="NickName" type='xsd:string'/>
    <part name="Image" type='xsd:string'/>
    <part name="SubIntro" type='xsd:string'/>
    <part name="SignText" type='xsd:string'/>
    <part name="Sex" type='xsd:string'/>
    <part name="EMail" type='xsd:string'/>
    <part name="Birth_Y" type='xsd:string'/>
    <part name="Birth_M" type='xsd:string'/>
    <part name="Birth_D" type='xsd:string'/>
    <part name="Tel" type='xsd:string'/>
    <part name="Address" type='xsd:string'/>
</message>
<message name="ModifyMemberResponse">
    <part name="ModifyMemberReturn" type='xsd:boolean'/>
</message>
<message name="QueryMemberData">
    <part name="UserName" type='xsd:string'/>
    <part name="SubIntroFlag" type='xsd:boolean'/>
</message>
<message name="QueryMemberDataResponse">
    <part name="QueryMemberDataReturn" type='xsd:string'/>
</message>
<message name="QueryNickName">
    <part name="UserName" type='xsd:string'/>
</message>
<message name="QueryNickNameResponse">
    <part name="QueryNickNameReturn" type='xsd:string'/>
</message>
<portType name="ServicePortType">
    <operation name="ChangePassword">
     <input message="typens:ChangePassword"/>
     <output message="typens:ChangePasswordResponse"/>
    </operation>
    <operation name="CheckMember">
     <input message="typens:CheckMember"/>
     <output message="typens:CheckMemberResponse"/>
    </operation>
    <operation name="DetectMember">
     <input message="typens:DetectMember"/>
     <output message="typens:DetectMemberResponse"/>
    </operation>
    <operation name="GetErrorMessage">
     <input message="typens:GetErrorMessage"/>
     <output message="typens:GetErrorMessageResponse"/>
    </operation>
    <operation name="GetmemberData">
     <input message="typens:GetmemberData"/>
     <output message="typens:GetmemberDataResponse"/>
    </operation>
    <operation name="GetMemberUserName">
     <input message="typens:GetMemberUserName"/>
     <output message="typens:GetMemberUserNameResponse"/>
    </operation>
    <operation name="JoinMember">
     <input message="typens:JoinMember"/>
     <output message="typens:JoinMemberResponse"/>
    </operation>
    <operation name="ModifyMember">
     <input message="typens:ModifyMember"/>
     <output message="typens:ModifyMemberResponse"/>
    </operation>
    <operation name="QueryMemberData">
     <input message="typens:QueryMemberData"/>
     <output message="typens:QueryMemberDataResponse"/>
    </operation>
    <operation name="QueryNickName">
     <input message="typens:QueryNickName"/>
     <output message="typens:QueryNickNameResponse"/>
    </operation>
</portType>
<binding name="ServiceBinding" type="typens:ServicePortType">
    <soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/>
    <operation name="ChangePassword">
     <soap:operation soapAction="urn:ServiceAction"/>
     <input>
      <soap:body namespace="urn:sooostar_forum" use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
     </input>
     <output>
      <soap:body namespace="urn:sooostar_forum" use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
     </output>
    </operation>
    <operation name="CheckMember">
     <soap:operation soapAction="urn:ServiceAction"/>
     <input>
      <soap:body namespace="urn:sooostar_forum" use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
     </input>
     <output>
      <soap:body namespace="urn:sooostar_forum" use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
     </output>
    </operation>
    <operation name="DetectMember">
     <soap:operation soapAction="urn:ServiceAction"/>
     <input>
      <soap:body namespace="urn:sooostar_forum" use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
     </input>
     <output>
      <soap:body namespace="urn:sooostar_forum" use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
     </output>
    </operation>
    <operation name="GetErrorMessage">
     <soap:operation soapAction="urn:ServiceAction"/>
     <input>
      <soap:body namespace="urn:sooostar_forum" use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
     </input>
     <output>
      <soap:body namespace="urn:sooostar_forum" use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
     </output>
    </operation>
    <operation name="GetmemberData">
     <soap:operation soapAction="urn:ServiceAction"/>
     <input>
      <soap:body namespace="urn:sooostar_forum" use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
     </input>
     <output>
      <soap:body namespace="urn:sooostar_forum" use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
     </output>
    </operation>
    <operation name="GetMemberUserName">
     <soap:operation soapAction="urn:ServiceAction"/>
     <input>
      <soap:body namespace="urn:sooostar_forum" use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
     </input>
     <output>
      <soap:body namespace="urn:sooostar_forum" use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
     </output>
    </operation>
    <operation name="JoinMember">
     <soap:operation soapAction="urn:ServiceAction"/>
     <input>
      <soap:body namespace="urn:sooostar_forum" use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
     </input>
     <output>
      <soap:body namespace="urn:sooostar_forum" use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
     </output>
    </operation>
    <operation name="ModifyMember">
     <soap:operation soapAction="urn:ServiceAction"/>
     <input>
      <soap:body namespace="urn:sooostar_forum" use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
     </input>
     <output>
      <soap:body namespace="urn:sooostar_forum" use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
     </output>
    </operation>
    <operation name="QueryMemberData">
     <soap:operation soapAction="urn:ServiceAction"/>
     <input>
      <soap:body namespace="urn:sooostar_forum" use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
     </input>
     <output>
      <soap:body namespace="urn:sooostar_forum" use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
     </output>
    </operation>
    <operation name="QueryNickName">
     <soap:operation soapAction="urn:ServiceAction"/>
     <input>
      <soap:body namespace="urn:sooostar_forum" use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
     </input>
     <output>
      <soap:body namespace="urn:sooostar_forum" use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
     </output>
    </operation>
</binding>
<service name="sooostar_forumService">
    <port name="ServicePort" binding="typens:ServiceBinding">
     <soap:address location="http://61.62.130.46/10/glofish_ms/webservice/service.php"/>
    </port>
</service>
</definitions>
4.编写soapclient.php,也就是soap客户端
$client = new SoapClient("http://localhost:81/discuz/soap_wsdl.wsdl");  
var_dump($client->__getFunctions());
//$PhotoList=$client->GetmemberData("puugee");
//print($PhotoList);
5.完成上述步骤后,可测试通过

你可能感兴趣的:(PHP,Web,service,SOAP,休闲)