PHP中soap的用法实例

yipeiwu_com5年前PHP代码库

本文实例讲述了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程序设计有所帮助。

相关文章

Ping服务的php实现方法,让网站快速被收录

这篇文章继续说说这个ping服务的问题,首先归纳和总结以下资料: 【1】手动Ping服务地址: Baidu(百度)地址: http://ping.baidu.com/ping.html...

ThinkPHP like模糊查询,like多匹配查询,between查询,in查询,一般查询书写方法

ThinkPHP的数据库条件查询语句有字符串式,数组式书写方法 字符串式即是原生式,数组式查询语句因书写方式与特定字符的原因比较复杂,下面为大家例出了常用的ThinkPHP数组式查询语句...

PHP 杂谈《重构-改善既有代码的设计》之四 简化条件表达式

PHP 杂谈《重构-改善既有代码的设计》之四 简化条件表达式

思维导图 点击下图,查看大图。  介绍    条件逻辑有可能十分复杂,因此本章提供一些重构的手法,专门用来简化它们。   全文简述(你可直接跳...

PHP使用feof()函数读文件的方法

本文实例讲述了PHP使用feof()函数读文件的方法。分享给大家供大家参考。具体用法如下: feof应用于PHP 4, PHP 5 -用来测试文件指针是否到了文件结束的位置。 如果服务...

PHP form 表单传参明细研究

为什么呢?因为表单的处理有GET和POST两种,怎么在这两种表单中都要传递一直do参数,而且要在后台通过同样的方式获取就一直是个很困惑的问题,今天经过几个小时的实验终于搞懂了,特发此文。...