>백엔드 개발 >PHP 튜토리얼 >웹서버 두 가지 모드

웹서버 두 가지 모드

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"?>
<wsdl: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/">
  <wsdl:types>
    <xsd:schema targetNamespace="http://localhost/interface/">
      <xsd:element name="HelloWorld">
        <xsd:complexType>
          <xsd:sequence>
            <xsd:element name="in" type="xsd:string"/>
          </xsd:sequence>
        </xsd:complexType>
      </xsd:element>
      <xsd:element name="HelloWorldResponse">
        <xsd:complexType>
          <xsd:sequence>
            <xsd:element name="out" type="xsd:string"/>
          </xsd:sequence>
        </xsd:complexType>
      </xsd:element>
      <xsd:element name="Add">
      	<xsd:complexType>
      		<xsd:sequence>
      			<xsd:element name="in" type="xsd:int"></xsd:element>
      		</xsd:sequence>
      	</xsd:complexType>
      </xsd:element>
      <xsd:element name="AddResponse">
      	<xsd:complexType>
      		<xsd:sequence>

      			<xsd:element name="out" type="xsd:int"></xsd:element>
      		</xsd:sequence>
      	</xsd:complexType>
      </xsd:element>
    </xsd:schema>
  </wsdl:types>
   <wsdl:message name="AddRequest">   	<wsdl:part name="a" type="xsd:int"></wsdl:part>
  	<wsdl:part name="b" type="xsd:int"></wsdl:part>
  </wsdl:message>
  <wsdl:message name="AddResponse">
  	<wsdl:part name="c" type="xsd:int"></wsdl:part>
  </wsdl:message>
  <wsdl:portType name="TestSoap">     <wsdl:operation name="Add">
    	<wsdl:input message="tns:AddRequest"></wsdl:input>
    	<wsdl:output message="tns:AddResponse"></wsdl:output>
    </wsdl:operation>
  </wsdl:portType>
  <wsdl:binding name="soapSOAP" type="tns:TestSoap">
  	<soap:binding style="document"
  		transport="http://schemas.xmlsoap.org/soap/http" />
  	<wsdl:operation name="Add">
  		<soap:operation soapAction="http://localhost/interface/Add" />
  		<wsdl:input>
  			<soap:body use="literal"
  				namespace="http://localhost/interface/" />
  		</wsdl:input>
  		<wsdl:output>
  			<soap:body use="literal"
  				namespace="http://localhost/interface/" />
  		</wsdl:output>
  	</wsdl:operation>
  </wsdl:binding>
  <wsdl:service name="TestSoap">
    <wsdl:port binding="tns:soapSOAP" name="soapSOAP">
      <soap:address location="http://localhost/interface/myservice.php"/>
    </wsdl:port>
  </wsdl:service>
</wsdl: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);
}
?>

저작권 표시: 이 글은 해당 블로거의 원본 글이므로 블로거의 허락 없이 복제할 수 없습니다.

위 내용을 포함해 웹서버의 두 가지 모드를 소개하고 있는데, PHP 튜토리얼에 관심이 있는 친구들에게 도움이 되었으면 좋겠습니다.

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.