通过PHP向c#生成的WSDL发出SOAP请求,与语法斗争

本文关键字:请求 SOAP 斗争 语法 发出 WSDL PHP 通过 | 更新日期: 2023-09-27 18:02:54

我有一个用c#编写的web服务。我从PHP调用这个web服务的方法。所有需要基本参数的方法都工作得很好。当我试图调用一个更复杂的方法时,我收到异常。

这是我的c#:

[WebMethod(Description = "Search components using the specified filters and username.")]
public DataSet Search(AttributeFilter[] filters, string username,
    int[] displayedAttributes, ComponentSearchOptions options,
    bool includeFlags, bool includeIcons)
{
    RemoteComponent remoteComponent = RemoteActivator.Create<RemoteComponent>();
    return remoteComponent.SearchComponents(filters, username, displayedAttributes, options, includeFlags, includeIcons);
}

下面是关联的WSDL:

<s:element name="Search">
    <s:complexType>
        <s:sequence>
            <s:element minOccurs="0" maxOccurs="1" name="filters" type="tns:ArrayOfAttributeFilter"/>
            <s:element minOccurs="0" maxOccurs="1" name="username" type="s:string"/>
            <s:element minOccurs="0" maxOccurs="1" name="displayedAttributes" type="tns:ArrayOfInt"/>
            <s:element minOccurs="1" maxOccurs="1" name="options" type="tns:ComponentSearchOptions"/>
            <s:element minOccurs="1" maxOccurs="1" name="includeFlags" type="s:boolean"/>
            <s:element minOccurs="1" maxOccurs="1" name="includeIcons" type="s:boolean"/>
        </s:sequence>
    </s:complexType>
</s:element>
<s:complexType name="ArrayOfAttributeFilter">
    <s:sequence>
        <s:element minOccurs="0" maxOccurs="unbounded" name="AttributeFilter" nillable="true" type="tns:AttributeFilter"/>
    </s:sequence>
</s:complexType>
<s:complexType name="AttributeFilter">
    <s:sequence>
        <s:element minOccurs="1" maxOccurs="1" name="Attribute" type="s:int"/>
        <s:element minOccurs="1" maxOccurs="1" name="Comparison" type="tns:SearchComparison"/>
        <s:element minOccurs="0" maxOccurs="1" name="Value"/>
    </s:sequence>
</s:complexType>
<s:simpleType name="SearchComparison">
    <s:restriction base="s:string">
        <s:enumeration value="Equals"/>
        <s:enumeration value="GreaterThan"/>
        <s:enumeration value="GreaterThanOrEqual"/>
        <s:enumeration value="In"/>
        <s:enumeration value="LessThan"/>
        <s:enumeration value="LessThanOrEqual"/>
        <s:enumeration value="Like"/>
    </s:restriction>
</s:simpleType>
<s:simpleType name="ComponentSearchOptions">
    <s:list>
        <s:simpleType>
            <s:restriction base="s:string">
                <s:enumeration value="None"/>
                <s:enumeration value="IncludeCables"/>
                <s:enumeration value="IncludePorts"/>
            </s:restriction>
        </s:simpleType>
    </s:list>
</s:simpleType>
<s:element name="SearchResponse">
    <s:complexType>
        <s:sequence>
            <s:element minOccurs="0" maxOccurs="1" name="SearchResult">
                <s:complexType>
                    <s:sequence>
                    <s:element ref="s:schema"/>
                    <s:any/>
                    </s:sequence>
                </s:complexType>
            </s:element>
        </s:sequence>
    </s:complexType>
</s:element>

这是我目前在PHP中的尝试:

$filters = array("filter" => (object)array("AttributeFilter" => (object)array("Attribute" => -2, "SearchComparison" => "Equals", "Value" => "110001")));
$search = $soapClient->Search($filters , "Administrator", NULL, "None", true, true);

,我收到异常:

SOAP-ERROR: Encoding: object has no 'options' property

谁能给我指个正确的方向?或者告诉我,如果我正在尝试的东西在技术上是不可能的?

谢谢。

哦,还有,这里有一个c#测试用例可以工作:

private readonly ComponentSearchServicesSoapClient ComponentSearchServices = new ComponentSearchServicesSoapClient();
[Test]
public void TestSearchBarcode()
{
    AttributeFilter filter = new AttributeFilter
    {
        Attribute = -2,
        Comparison = SearchComparison.Equals,
        Value = "110001"
    };
    DataSet dataset = ComponentSearchServices.Search(new[] { filter }, "Administrator", null, ComponentSearchOptions.None, false, false);
    if (dataset.Tables.Count == 0 || dataset.Tables[0].Rows.Count != 1)
    {
        throw new Exception("Failed to properly search");
    }
}

通过PHP向c#生成的WSDL发出SOAP请求,与语法斗争

解决方案分为两部分。

调用的正确语法是:

$search = $soapClient->Search(array("filters" => array("AttributeFilter" => array('Attribute' => 310, 'Comparison' => 'Equals', 'Value' => 'Peering LAN')), 'username' => 'Administrator', 'displayedAttributes' => array(310), 'options' => 'None', 'includeFlags' => true, 'includeIcons' => true));

附加问题是AttributeFilter类期望属性"Value"是"object"类型。将'Value'更改为'string'类型删除了正在发生的序列化异常。