views:

229

answers:

2

I am not sure how to correctly make a fault element in a wsdl file repesenting a PHP exception.

I have created a PHP web service that throws an exception for testing purposes. When I call this web service in a test C# project, I get a reflection exception with the message "Item has already been added. Key in dictionary: 'System.Object' Key being added: 'System.Object'".

So this obviously means I have not created the fault element correctly in the wsdl file.

A: 

I don't know about PHP, or how obvious it is that you made a mistake in the creation of your fault element. Nor do I know what your wsdl or your php looks like, but here's an example of a wsdl with a fault message that works:

<?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"/&gt;
    <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"/&gt;
        </wsdl:port>
    </wsdl:service>
</wsdl:definitions>

Regards, Miel.

Miel
A: 

I think it's better to return oneself the fault message with http status code = 200 and content-type = text/xml. Thus, you can catch fault message within Flash and 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/"&gt;
    <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;
    }
}
ilyas