如何在wsdl文件中支持PHP异常

时间:2009-11-17 09:25:54

标签: php exception web-services wsdl

我不确定如何正确地在wsdl文件中创建一个错误元素来重复PHP异常。

我创建了一个PHP Web服务,它会抛出异常以进行测试。当我在一个测试C#项目中调用这个Web服务时,我得到一个反射异常,消息“Item已经被添加。键入字典:'System.Object'键被添加:'System.Object'”。

所以这显然意味着我没有在wsdl文件中正确创建fault元素。

2 个答案:

答案 0 :(得分:0)

我不了解PHP,或者你在创建fault元素时出错是多么明显。我也不知道你的wsdl或你的php是什么样的,但是这里有一个带有错误消息的wsdl的例子:

<?xml version="1.0"?>
<wsdl:definitions xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
                  xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
                  xmlns:xsd="http://www.w3.org/2001/XMLSchema"
                  xmlns:wsaw="http://www.w3.org/2006/05/addressing/wsdl"
                  xmlns:tns="http://www.your.site/YourService"
                  targetNamespace="http://www.your.site/YourService"
                  name="ays">
    <xsd:import schemaLocation="http://www.your.site/YourService/AtYourService.xsd"
                namespace="http://www.your.site/YourService"/>
    <wsdl:message name="Input">
        <wsdl:part name="parameters"
                   element="tns:Question"/>
    </wsdl:message>

    <wsdl:message name="Output">
        <wsdl:part name="info"
                   element="tns:Answer"/>
    </wsdl:message>

    <wsdl:message name="Fault">
        <wsdl:part name="detail"
                   element="tns:FaultMessage"/>
    </wsdl:message>

    <wsdl:portType name="YourPortType">
        <wsdl:operation name="Question">
            <wsdl:input wsaw:Action="http://www.your.site/YourService/Question"
                        message="tns:Input"/>
            <wsdl:output wsaw:Action="http://www.your.site/YourService/Answer"
                         message="tns:Output"/>
            <wsdl:fault wsaw:Action="http://www.your.site/YourService/Fault"
                        name="QuestionFault"
                        message="tns:Fault"/>
        </wsdl:operation>
    </wsdl:portType>

    <wsdl:binding name="YourBinding"
                  type="tns:YourPortType">
        <wsdl:operation name="Question">
            <soap:operation soapAction="http://www.your.site/YourService/Question" style="document"/>
            <wsdl:input>
                <soap:body use="literal"/>
            </wsdl:input>
            <wsdl:output>
                <soap:body use="literal"/>
            </wsdl:output>
            <wsdl:fault name="QuestionFault">
                <soap:fault name="QuestionFault" use="literal"/>
            </wsdl:fault>
        </wsdl:operation>
    </wsdl:binding>

    <wsdl:service name="YourService">
        <wsdl:port name="YourBinding" binding="tns:YourBinding">
            <soap:address location="http://www.your.site/YourService"/>
        </wsdl:port>
    </wsdl:service>
</wsdl:definitions>

问候,Miel。

答案 1 :(得分:0)

我认为最好使用http状态代码= 200和content-type = text / xml返回自己的错误消息。因此,您可以在Flash和Flex中捕获错误消息

    header("status: 200");
    header("Content-Type: text/xml; charset=utf-8");
    try {
         $wsdl = "http://wsdluri";
         $serverConfig = array("soap_version"=> SOAP_1_2, "encoding" => "UTF-8");
         $server = new SoapServer($wsdl, $serverConfig);
         $server->setObject($myService);
         $server->handle($HTTP_RAW_POST_DATA);
    } catch (Exception $exception) {
    $xmlstr =
    <<<XML
    <?xml version="1.0" encoding="UTF-8"?>
    <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
    <SOAP-ENV:Body>
    <SOAP-ENV:Fault>
    <faultcode>{$exception->getCode()}</faultcode>
    <faultstring>{$exception->getMessage()}</faultstring>
    <detail><![CDATA[{$exception->getTraceAsString()}]]></detail>
    </SOAP-ENV:Fault>
    </SOAP-ENV:Body>
    </SOAP-ENV:Envelope>
    XML;
    echo $xmlstr;
    }
}