PHP中soap的用法实例

作者:shichen2014 时间:2023-11-14 09:40:24 

本文实例讲述了PHP中soap的用法,分享给大家供大家参考。具体用法分析如下:

PHP 使用soap有两种方式。

一、用wsdl文件

服务器端:

<?php
class service
{
  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文件。

<?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('http://localhost/interface/soap.wsdl');
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('uri' => "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程序设计有所帮助。

标签:PHP,soap
0
投稿

猜你喜欢

  • python使用docx模块读写docx文件的方法与docx模块常用方法详解

    2022-05-14 11:37:17
  • Go语言中调用外部命令的方法总结

    2024-05-13 10:44:09
  • Python之两种模式的生产者消费者模型详解

    2021-07-31 17:44:02
  • 使用Python在Windows下获取USB PID&VID的方法

    2021-02-15 19:31:20
  • Python爬虫之批量下载喜马拉雅音频

    2022-09-25 20:18:27
  • Python登录注册验证功能实现

    2022-10-17 04:53:00
  • 详解Go flag实现二级子命令的方法

    2024-04-25 13:19:37
  • openCV实现图像融合的示例代码

    2022-05-20 03:28:16
  • 轻松解决SQL Server 2005中的常见问题

    2008-11-28 14:11:00
  • CSS技巧之圆角背景与三角形

    2010-10-19 12:40:00
  • js导出格式化的excel 实例方法

    2024-04-10 16:17:15
  • 详解python 模拟豆瓣登录(豆瓣6.0)

    2022-06-29 22:20:00
  • Python Pandas数据合并pd.merge用法详解

    2022-07-18 22:15:47
  • ASP.NET教程第二讲 ASP.NET学习

    2007-08-07 12:01:00
  • 一款Python工具制作的动态条形图(强烈推荐!)

    2021-07-21 17:38:18
  • 2行css代码屏蔽网页挂马

    2008-09-29 18:54:00
  • Python如何读取、写入CSV数据

    2022-02-17 14:03:31
  • Oracle下的Java分页功能_动力节点Java学院整理

    2024-01-20 16:34:00
  • python机器学习理论与实战(一)K近邻法

    2021-08-27 18:05:49
  • Python数据分析之 Pandas Dataframe应用自定义

    2023-03-02 09:18:12
  • asp之家 网络编程 m.aspxhome.com