WCF服务PHP客户端连接


WCF Service PHP Client Connection

我已经尝试了很长时间连接到这样的web服务:

<wsdl:definitions xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:wsam="http://www.w3.org/2007/05/addressing/metadata" xmlns:wsx="http://schemas.xmlsoap.org/ws/2004/09/mex" xmlns:wsap="http://schemas.xmlsoap.org/ws/2004/08/addressing/policy" xmlns:msc="http://schemas.microsoft.com/ws/2005/12/wsdl/contract" xmlns:wsp="http://schemas.xmlsoap.org/ws/2004/09/policy" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:tns="http://tempuri.org/" xmlns:wsa10="http://www.w3.org/2005/08/addressing" xmlns:wsaw="http://www.w3.org/2006/05/addressing/wsdl" xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/08/addressing" name="Service1" targetNamespace="http://tempuri.org/">
<wsdl:types>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" targetNamespace="http://tempuri.org/">
<xs:import namespace="http://schemas.datacontract.org/2004/07/WcfService1"/>
<xs:element name="JsonWebMessage">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="0" name="message" nillable="true" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="JsonWebMessageResponse">
<xs:complexType>
<xs:sequence>
<xs:element xmlns:q1="http://schemas.datacontract.org/2004/07/WcfService1" minOccurs="0" name="JsonWebMessageResult" nillable="true" type="q1:ServiceResult"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://schemas.microsoft.com/2003/10/Serialization/" attributeFormDefault="qualified" elementFormDefault="qualified" targetNamespace="http://schemas.microsoft.com/2003/10/Serialization/">
<xs:element name="anyType" nillable="true" type="xs:anyType"/>
<xs:element name="anyURI" nillable="true" type="xs:anyURI"/>
<xs:element name="base64Binary" nillable="true" type="xs:base64Binary"/>
<xs:element name="boolean" nillable="true" type="xs:boolean"/>
<xs:element name="byte" nillable="true" type="xs:byte"/>
<xs:element name="dateTime" nillable="true" type="xs:dateTime"/>
<xs:element name="decimal" nillable="true" type="xs:decimal"/>
<xs:element name="double" nillable="true" type="xs:double"/>
<xs:element name="float" nillable="true" type="xs:float"/>
<xs:element name="int" nillable="true" type="xs:int"/>
<xs:element name="long" nillable="true" type="xs:long"/>
<xs:element name="QName" nillable="true" type="xs:QName"/>
<xs:element name="short" nillable="true" type="xs:short"/>
<xs:element name="string" nillable="true" type="xs:string"/>
<xs:element name="unsignedByte" nillable="true" type="xs:unsignedByte"/>
<xs:element name="unsignedInt" nillable="true" type="xs:unsignedInt"/>
<xs:element name="unsignedLong" nillable="true" type="xs:unsignedLong"/>
<xs:element name="unsignedShort" nillable="true" type="xs:unsignedShort"/>
<xs:element name="char" nillable="true" type="tns:char"/>
<xs:simpleType name="char">
<xs:restriction base="xs:int"/>
</xs:simpleType>
<xs:element name="duration" nillable="true" type="tns:duration"/>
<xs:simpleType name="duration">
<xs:restriction base="xs:duration">
<xs:pattern value="'-?P('d*D)?(T('d*H)?('d*M)?('d*('.'d*)?S)?)?"/>
<xs:minInclusive value="-P10675199DT2H48M5.4775808S"/>
<xs:maxInclusive value="P10675199DT2H48M5.4775807S"/>
</xs:restriction>
</xs:simpleType>
<xs:element name="guid" nillable="true" type="tns:guid"/>
<xs:simpleType name="guid">
<xs:restriction base="xs:string">
<xs:pattern value="['da-fA-F]{8}-['da-fA-F]{4}-['da-fA-F]{4}-['da-fA-F]{4}-['da-fA-F]{12}"/>
</xs:restriction>
</xs:simpleType>
<xs:attribute name="FactoryType" type="xs:QName"/>
<xs:attribute name="Id" type="xs:ID"/>
<xs:attribute name="Ref" type="xs:IDREF"/>
</xs:schema>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://schemas.datacontract.org/2004/07/WcfService1" elementFormDefault="qualified" targetNamespace="http://schemas.datacontract.org/2004/07/WcfService1">
<xs:complexType name="ServiceResult">
<xs:sequence>
<xs:element minOccurs="0" name="IsValid" type="xs:boolean"/>
<xs:element minOccurs="0" name="ReturnJson" nillable="true" type="xs:string"/>
</xs:sequence>
</xs:complexType>
<xs:element name="ServiceResult" nillable="true" type="tns:ServiceResult"/>
</xs:schema>
</wsdl:types>
<wsdl:message name="IService1_JsonWebMessage_InputMessage">
<wsdl:part name="parameters" element="tns:JsonWebMessage"/>
</wsdl:message>
<wsdl:message name="IService1_JsonWebMessage_OutputMessage">
<wsdl:part name="parameters" element="tns:JsonWebMessageResponse"/>
</wsdl:message>
<wsdl:portType name="IService1">
<wsdl:operation name="JsonWebMessage">
<wsdl:input wsaw:Action="http://tempuri.org/IService1/JsonWebMessage" message="tns:IService1_JsonWebMessage_InputMessage"/>
<wsdl:output wsaw:Action="http://tempuri.org/IService1/JsonWebMessageResponse" message="tns:IService1_JsonWebMessage_OutputMessage"/>
</wsdl:operation>
</wsdl:portType>
<wsdl:service name="Service1"/>
</wsdl:definitions>

根据wsdl xml文档,该服务有一个名为JsonWebMessage的操作,该操作接受字符串作为输入(字符串化的JSON)。我使用以下PHP代码访问以下服务:

<?php
if( isset( $_SERVER['HTTP_X_REQUESTED_WITH'] ) && ( $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest' ) )
{
    $message = $_GET['message'];
    $wcf = new SoapClient('URL_HERE/Service1.svc?singleWsdl');
    $sonuc = $wcf->JsonWebMessage(array("message" => $message));
    echo $sonuc;
}
?>

但是无论我怎么做,我都无法连接到服务。我尝试使用'URL_HERE/Service1。Svc/ws’作为地址无效。等待请求一段时间后,响应是500内部服务器错误。URL_HERE包含端口号8100,如下所示:URL_HERE:8100/Service1.svc。当我打开错误报告时,从php得到的错误消息是:

Fatal error: Uncaught SoapFault exception: [WSDL] SOAP-ERROR: Parsing WSDL: Couldn't load from 'URL_HERE/Service1.svc?singleWsdl' : failed to load external entity "http://URL_HERE/Service1.svc?singleWsdl" in SOURCE_URL/formtercuman.php:10Stack trace:#0 SOURCE_URL/formtercuman.php(10): SoapClient->SoapClient('http://....')#1 {main} thrown in SOURCE_URL/formtercuman.php on line 10

问题解决。此问题与windows IIS服务器的防火墙设置有关。我们不知道到底是哪里出了问题,但我们完全禁用了防火墙,并决定使用一台没有连接问题的强化防火墙机器。谢谢。

服务器-客户端连接已解析。我已经过了你之前发布的致命错误。

连接后显示版本错误。

错误显示"[faultstring] =>错误版本,[faultcode] => VersionMismatch"。您的SoapClient行:

$wcf = new SoapClient('URL_HERE/Service1.svc?singleWsdl');

应该如下所示(取自http://www.php.net/manual/en/soapclient.soapclient.php):

$wcf = new SoapClient(null, array('location' => "http://localhost/Service1.svc",
        'soap_version' => SOAP_1_2,
            'uri'      => "http://test-uri/",
            'style'    => SOAP_DOCUMENT,
            'use'      => SOAP_LITERAL));

如果我是你,我会用试着抓住的方式把事情做好,像:

try{
    $wcf = new SoapClient(null, array('location' => "http://localhost/Service1.svc",
        'soap_version' => SOAP_1_2,
            'uri'      => "http://test-uri/",
            'style'    => SOAP_DOCUMENT,
            'use'      => SOAP_LITERAL));
    $sonuc = $wcf->JsonWebMessage(array("message" => $message));
    echo $sonuc;
} catch (Exception $e){
    print('<pre>');
    print_r($e);
    print('</pre>');
}

您可以查找WSDL的正确版本,并在选项中以相同的方式设置它。