PHP 5 SoapServer 用法?

标签 php web-services soap

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

但是,关于此类的文档非常少,我不知道如何创建一个服务器,只是一个客户端。任何人都可以提供一些 tuts 或示例代码吗?

最佳答案

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

String values returned by PHP SoapServer not received by .Net client

但是,虽然我无法让这个基本示例正常工作,但我已经设法让一个返回自定义对象数组的示例正常工作,所以我将在这里分享这个示例,希望它对其他人有所帮助。此示例定义了一个单独的操作 getUsers,它采用一个 string 参数 message,只是为了演示消息传递。此操作返回一组 User 对象。 User 对象还有一个字段 message,我在其中将接收到的值传回服务,仅用于测试目的。我将发布完整的示例; 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>

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());
}
?>

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);
            }
        }
    }
}

就是这样。我希望这个示例对其他人有用,并为他们节省我为此花费的时间!!

关于PHP 5 SoapServer 用法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7748753/

相关文章:

java - 从 WSDL 生成类 : Duplicate operation with name error

java - { "readyState": 0 ,"status": 0 ,"statusText": "NetworkError: Failed to execute ' send' on 'XMLHttpRequest' : Failed to load 'http://localhost:8088/' .“}

java - 如何使用数字证书创建Web服务

php - 如何在 php 中读取传入的 SOAP 请求

php - 方法: how to change URL without reloading page and index whole URL (with anchor # part)?

php - 将图像从iOS上传到PHP脚本

php - 使用 PHP 解析实时 SOAP 数据流

java - 如何使用 JAX-WS 客户端指定 WS-Addressing 版本?

PHP require 语句 - 失败,但抛出 E_WARNING 而不是 E_COMPILE_ERROR

php - 如何解决paypal交易的IPN fatal error ?