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程序设计有所帮助。

相关文章

PHP中冒号、endif、endwhile、endfor使用介绍

我们经常在wordpress一类博客程序的模板里面看到很多奇怪的PHP语法,比如: 复制代码 代码如下: <?php if(empty($GET_['a'])): ?> &l...

php email邮箱正则

1.验证email: < ?php    if (ereg("/^[a-z]([a-z0-9]*[-_\.]?[a-z0-9]+)*@([a-z0-9]*[...

PHP和Java 集成开发详解分析 强强联合第1/4页

时间一天天过去,这两个亮点也变得越来越亮,很快,它们受到了编程者的喜欢,于是有人有疑问了:要是它们两者相遇,会发生什么事情?有没有可能将它们的强项结合在一起呢? 尝试在PHP和Java之...

PHP的命令行命令使用指南

 当用户打开php页面时,服务端便执行PHP的命令并将执行结果发送至用户的浏览器中,这类似于ASP和CoildFusion,PHP可以运行在WINDOWS和多种版本的UNIX上...

PHP 常用时间函数资料整理

php常用的时间函数 测试环境:php5.3.29 unix时间戳(从Unix 纪元(January 1 1970 00:00:00 GMT)到给定时间的秒数。)。以下简称时间戳。 返回...