日期:2010-02-27  浏览次数:20466 次

但是问题还是要解决的,期限就摆在眼前呢。经过一番调查,终于有了眉目,现在分享给大家。

首先要说明的,PHP服务器需要至少需要两个文件——一个WSDL文件和一个PHP文件。WSDL文件是一种机读的XML文件,用于描述WebService提供的服务和调用方法(对于.NET则可以自动生成调用代码,十分好用),php文件就是真正实现的WEB服务了。

1)PHP服务器端代码

1-1)TestWebService.php代码

以下为引用的内容:

<?php
class TestWebService
{
    public function HelloWorld()
    {
        return array("HelloWorldResult"=>"Hello");
    }

    public function GetArray($args)
        {
          /*
           注意,Web Service的方法在声明时至多一个参数,
            可是在调用该方法时就必须传value1,value2两个参数。
            (这一点十分令人费解,我的理解是,在调用该方法时,系统把所有参数都放到一个对象里传过来的)
          */

        $value1 = $args->value1; 
        $value2 = $args->value2;//这两句是获取真正的参数
 
        $arry = array($value1,$value2);

        //返回值也很特别,不是直接返回$arry,而是把它放到一个对象里再返回。
        return array("GetArrayResult"=>$arry);
    }
}

//创建WebSevice实例
$server = new SoapServer("TestWebService.wsdl");
//指定类名
$server->setClass("TestWebService");

$server->handle();


?>
 


1-2)TestWebService.wsdl代码

以下为引用的内容:

<?xml version="1.0" encoding="utf-8"?>
<wsdl:definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tm="http://microsoft.com/wsdl/mime/textMatching/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/" xmlns:tns="http://tempuri.org/" xmlns:s="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" targetNamespace="http://tempuri.org/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">
  <wsdl:types>
    <s:schema elementFormDefault="qualified" targetNamespace="http://tempuri.org/">
      <s:element name="HelloWorld">
        <s:complexType />
      </s:element>
      <s:element name="HelloWorldResponse">
        <s:complexType>
          <s:sequence>
            <s:element minOccurs="0" maxOccurs="1" name="HelloWorldResult" type="s:string" />
          </s:sequence>
        </