首页 >后端开发 >php教程 >webserver 两种模式

webserver 两种模式

WBOY
WBOY原创
2016-08-08 09:20:58850浏览
PHP 使用soap有两种方式。

一、用wsdl文件

服务器端。

<?php class service
{
  public function HelloWorld()
   {
      return  "Hello";
   }
  public  function Add($a,$b)
   {
      return $a+$b;
   }
}
$server=new SoapServer(&#39;soap.wsdl&#39;,array(&#39;soap_version&#39; => SOAP_1_2));
$server->setClass("service");
$server->handle();
?>
资源描述文件,可以用工具(zend studio)生成。其实就是一个xml文件。
<?xml version="1.0" encoding="UTF-8"?>
<definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://localhost/interface/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="soap" targetnamespace="http://localhost/interface/">
  <types>
    <schema targetnamespace="http://localhost/interface/">
      <element name="HelloWorld">
        <complextype>
          <sequence>
            <element name="in" type="xsd:string"></element>
          </sequence>
        </complextype>
      </element>
      <element name="HelloWorldResponse">
        <complextype>
          <sequence>
            <element name="out" type="xsd:string"></element>
          </sequence>
        </complextype>
      </element>
      <element name="Add">
      	<complextype>
      		<sequence>
      			<element name="in" type="xsd:int"></element>
      		</sequence>
      	</complextype>
      </element>
      <element name="AddResponse">
      	<complextype>
      		<sequence>

      			<element name="out" type="xsd:int"></element>
      		</sequence>
      	</complextype>
      </element>
    </schema>
  </types>
   <message name="AddRequest">   	<part name="a" type="xsd:int"></part>
  	<part name="b" type="xsd:int"></part>
  </message>
  <message name="AddResponse">
  	<part name="c" type="xsd:int"></part>
  </message>
  <porttype name="TestSoap">     <operation name="Add">
    	<input message="tns:AddRequest">
    	<output message="tns:AddResponse"></output>
    </operation>
  </porttype>
  <binding name="soapSOAP" type="tns:TestSoap">
  	<binding style="document" transport="http://schemas.xmlsoap.org/soap/http"></binding>
  	<operation name="Add">
  		<operation soapaction="http://localhost/interface/Add"></operation>
  		<input>
  		<output>
  			<body use="literal" namespace="http://localhost/interface/"></body>
  		</output>
  	</operation>
  </binding>
  <service name="TestSoap">
    <port binding="tns:soapSOAP" name="soapSOAP">
      <address location="http://localhost/interface/myservice.php"></address>
    </port>
  </service>
</definitions>
客户端调用
<?php $soap = new SoapClient(&#39;http://localhost/interface/soap.wsdl&#39;);
echo $soap->Add(1,2);
?>
二、不用wsdl文件

服务器端

<?php class service
{
  public function HelloWorld()
   {
      return  "Hello";
   }
  public  function Add($a,$b)
   {
      return $a+$b;
   }
}
$server=new SoapServer(null,array(&#39;uri&#39; => "abcd"));
$server->setClass("service");
$server->handle();
?>
客户端
<?php 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);
}
?>

版权声明:本文为博主原创文章,未经博主允许不得转载。

以上就介绍了webserver 两种模式,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

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