PHP 5 SoapServer用法?

vin*_*nux 11 php soap web-services

我正在尝试使用本机SoapServer类在PHP中创建一个简单的SOAP Web服务:http://www.php.net/manual/en/class.soapserver.php

但是,这个类的文档很差,我不知道如何创建一个服务器,只是一个客户端.任何人都可以提供一些tuts或示例代码吗?

Bru*_*ill 9

我也一直在努力解决这个问题,尤其是获得与.Net客户端兼容的代码.我找到了适用于PHP客户端的示例,但是当我尝试从.Net客户端调用服务时,这些示例通常会失败.我仍然在努力解决这个问题,因此我的一个问题是在一个简单的PHP SoapServer示例中寻求帮助,该示例返回一个字符串值:

PHP SoapServer返回的字符串值未被.Net客户端接收

但是,虽然我无法使这个基本示例正常工作,但我设法得到一个返回自定义对象数组的示例,所以我将在这里分享这个例子,希望它对其他人有所帮助.此示例定义了一个单独的操作getUsers,它接受一个字符串参数消息,只是为了演示消息传递.此操作返回User对象的数组.所述用户对象还具有一个字段消息,我传回接收到的服务的价值,只是为了测试目的.我将发布完整的例子; wsdl文档,PHP服务器代码和C#客户端代码.

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://test-uri/soap/export/"
    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://test-uri/soap/export/"
    xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">

<wsdl:types>
<s:schema targetNamespace="http://test-uri/soap/export/" elementFormDefault="qualified">
    <s:import namespace="http://microsoft.com/wsdl/types/"/>

    <s:element name="getUsers">
        <s:complexType>
            <s:sequence>
                <s:element minOccurs="1" maxOccurs="1" name="message" type="s:string"/>
            </s:sequence>
        </s:complexType>
    </s:element>

    <s:element name="getUsersResponse">
        <s:complexType>
            <s:sequence>
                <s:element name="getUsersArray" type="tns:getUsersArray"/>
            </s:sequence>
        </s:complexType>
    </s:element>

    <s:complexType name="getUsersArray">
        <s:sequence>
            <s:element minOccurs="0" maxOccurs="unbounded" name="User" nillable="true" type="tns:User" />
        </s:sequence>
    </s:complexType>

    <s:complexType name="User">
        <s:sequence>
            <s:element minOccurs="1" maxOccurs="1" name="id" type="s:string"/>
            <s:element minOccurs="1" maxOccurs="1" name="firstname" type="s:string"/>
            <s:element minOccurs="1" maxOccurs="1" name="surname" type="s:string"/>
            <s:element minOccurs="1" maxOccurs="1" name="message" type="s:string"/>
        </s:sequence>
    </s:complexType>
</s:schema>
</wsdl:types>

<wsdl:message name="getUsersSoapIn">
<wsdl:part name="parameters" element="tns:getUsers"/>
</wsdl:message>
<wsdl:message name="getUsersSoapOut">
<wsdl:part name="parameters" element="tns:getUsersResponse"/>
</wsdl:message>

<wsdl:portType name="TestSoap">
<wsdl:operation name="getUsers">
    <wsdl:documentation xmlns:wsdl='http://schemas.xmlsoap.org/wsdl/'>
        Function ("getUsers")
    </wsdl:documentation>
    <wsdl:input message="tns:getUsersSoapIn"/>
    <wsdl:output message="tns:getUsersSoapOut"/>
</wsdl:operation>
</wsdl:portType>

<wsdl:portType name="TestSoap12">
<wsdl:operation name="getUsers">
    <wsdl:documentation xmlns:wsdl='http://schemas.xmlsoap.org/wsdl/'>
        Function ("getUsers")
    </wsdl:documentation>
    <wsdl:input message="tns:getUsersSoapIn"/>
    <wsdl:output message="tns:getUsersSoapOut"/>
</wsdl:operation>
</wsdl:portType>

<wsdl:binding name="TestSoap" type="tns:TestSoap">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="getUsers">
    <soap:operation soapAction="http://test-uri/soap/export/getUsers" style="document"/>
    <wsdl:input>
        <soap:body use="literal"/>
    </wsdl:input>
    <wsdl:output>
        <soap:body use="literal"/>
    </wsdl:output>
</wsdl:operation>
</wsdl:binding>

<wsdl:binding name="TestSoap12" type="tns:TestSoap12">
<soap12:binding transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="getUsers">
    <soap12:operation soapAction="http://test-uri/soap/export/getUsers" style="document"/>
    <wsdl:input>
        <soap12:body use="literal"/>
    </wsdl:input>
    <wsdl:output>
        <soap12:body use="literal"/>
    </wsdl:output>
</wsdl:operation>
</wsdl:binding>

<wsdl:service name="TestService">
<wsdl:port name="TestPort" binding="tns:TestSoap">
    <soap:address location="http://url/to/test_server.php"/>
</wsdl:port>
<wsdl:port name="TestSoap12" binding="tns:TestSoap12">
    <soap12:address location="http://url/to/test_server.php"/>
</wsdl:port>
</wsdl:service>

</wsdl:definitions>
Run Code Online (Sandbox Code Playgroud)

PHP服务器代码

<?php
function getUsers($args) {
   $args = (array)$args;
   return array("getUsersArray" => array(  
                                       array("id"=>"1",
                                           "firstname"=>"Barney",
                                           "surname"=>"Rubble",
                                           "message"=>$args["message"]), 
                                       array("id"=>"2", 
                                            "firstname"=>"Fred", 
                                            "surname"=>"Flintstone", 
                                            "message"=>$args["message"])
                                    )
               );
}
ini_set("soap.wsdl_cache_enabled", "0"); // disabling WSDL cache
$server = new SoapServer("test.wsdl");
$server->addFunction("getUsers");
try {
    $server->handle();
}
catch (Exception $e) {
    $server->fault('Sender', $e->getMessage());
}
?>
Run Code Online (Sandbox Code Playgroud)

C#客户端代码

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            WebReference.TestService srv = new WebReference.TestService();
            WebReference.User[] users = srv.getUsers("says hello");
            foreach (WebReference.User user in users)
            {
                MessageBox.Show(user.firstname+" "+user.message);
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

而已.我希望这个例子对其他人有用,并节省他们花费我的时间!