我从使用PHP编写的Web应用程序连接到SOAP Web服务,我遇到了让服务接受我试图发送它的对象的问题。但有时候,我无法弄清楚为什么特定物体成功通过而其他物体没有成功通过。
我正在使用PHP的SoapClient类,如果我使用__getFunctions()和__getTypes()方法,我得到以下响应:(为了简洁起见,我不会列出每个方法和类型)
这是我想在webservice中使用的postOrder方法:
string(323) "respOrder postOrder(string $siteId, string $secretKey, string $language, string $customerName, string $emailAddress, parmAddress $deliveryAddress, parmAddress $billingAddress, parmCreditCard $creditCard, string $cvv, parmOrderLineArray $orderLines, string $shippingCharge, string $reference, string $poNumber, string $text)"
这是一个给我带来麻烦的对象的例子,parmAddress:
[1]=>
string(176) "struct parmAddress {
string addressLine1;
string addressLine2;
string addressLine3;
string city;
string countryCode;
string name;
string postalCode;
string stateCode;
}
我试图以多种不同的方式构建这个特定的对象。使用PHP stdObject,创建名为parmAddress的PHP类的对象,甚至发送数组。不幸的是,我从Web服务获得的响应总是如下。在这个例子中,我只是传递一个我在下面定义的类型的对象:
//The type of object I send
class parmAddress{
public $addressLine1 = '891 Prairie Grass Drive';
public $addressLine2 = '';
public $addressLine3 = '';
public $city = 'Las Vegas';
public $countryCode = '1';
public $name = 'Eric Dodson';
public $postalCode = '89123';
public $stateCode = 'NV';
}
//Or I've tried the stdObject as mentioned above
$obj = new stdClass();
$obj->addressLine1= '';
$obj->addressLine2= '';
$obj->addressLine3 = '';
$obj->city = 'Las Vegas';
$obj->countryCode = '1';
$obj->name = 'Eric Dodson';
$obj->postalCode = '89123';
$obj->stateCode = 'NV';
//Making the object and using the postOrder() method
$parmaddress = new parmAddress();
$order = $client->postOrder('user', 'pass', 'en', 'Eric Dodson', 'dodson@dodson.com', $parmaddress, $parmaddress, $parmcreditcard, '123', $parmorderlinearray, 0.00, 'No Comments');
//The response given to me by the webservice
object(stdClass)#6 (3) {
["code"]=>
string(7) "failure"
["message"]=>
string(21) "Invalid address name."
["orderNumber"]=>
int(0)
}
奇怪的是,我已经成功发送其他对象,看起来完全一样。例如,如果我以相同的方式创建parmPart对象,并在它要求的parmPartArray中使用此getParts()方法传递它,则它是成功的。
$obj = new stdClass();
$obj->availabilityCode = '';
$obj->availableQuantity = '';
$obj->listPrice = '';
$obj->manufacturerCode = 'RPL';
$obj->partNumber = '341241';
$obj->price = '';
$obj->substitute = '';
$partsarray = array(
0 => $obj
);
$partsresponsearray = $client->getParts('user', 'pass', 'en', $partsarray);
我认为重要的是要注意网络服务想要发送我不太熟悉的结构,我希望我的问题就在于此。 Web服务的开发人员能够告诉我以下内容,但是我无法找到有关将对象从PHP发送到需要结构的Web服务的任何其他信息:
“当地址名称属性未填充时发出该消息。我记得像parmAddress这样的复合类型,你必须在PHP中”编码“它。我现在不确定语法。”
显然,Web服务认为我的“name”字符串是空的,并且我必须做一些类型的编码才能让它识别我发送它的内容。有任何想法吗?谢谢,我非常感谢你的帮助!
根据其他用户的要求,这里是WSDL:
<?xml version='1.0' encoding='UTF-8'?><!-- Published by JAX-WS RI at http://jax-ws.dev.java.net.
RI's version is JAX-WS RI 2.1.7-b01-. --><!-- Generated by JAX-WS RI at http://jax-ws.dev.java.net.
RI's version is JAX-WS RI 2.1.7-b01-. -->
<definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
xmlns:tns="http://ws.reliableparts.net/" xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://ws.reliableparts.net/"
name="RetailOrderServerService">
<types>
<xsd:schema>
<xsd:import namespace="http://ws.reliableparts.net/"
schemaLocation="http://www.reliableparts.net/dataservicetc/retail?xsd=1" />
</xsd:schema>
</types>
<message name="getParts">
<part name="siteId" type="xsd:string" />
<part name="secretKey" type="xsd:string" />
<part name="language" type="xsd:string" />
<part name="parts" type="tns:parmPartArray" />
</message>
<message name="getPartsResponse">
<part name="return" type="tns:parmPartArray" />
</message>
<message name="postOrder">
<part name="siteId" type="xsd:string" />
<part name="secretKey" type="xsd:string" />
<part name="language" type="xsd:string" />
<part name="customerName" type="xsd:string" />
<part name="emailAddress" type="xsd:string" />
<part name="deliveryAddress" type="tns:parmAddress" />
<part name="billingAddress" type="tns:parmAddress" />
<part name="creditCard" type="tns:parmCreditCard" />
<part name="cvv" type="xsd:string" />
<part name="orderLines" type="tns:parmOrderLineArray" />
<part name="shippingCharge" type="xsd:string" />
<part name="reference" type="xsd:string" />
<part name="poNumber" type="xsd:string" />
<part name="text" type="xsd:string" />
</message>
<message name="postOrderResponse">
<part name="return" type="tns:respOrder" />
</message>
<portType name="RetailOrderServer">
<operation name="getParts" parameterOrder="siteId secretKey language parts">
<input message="tns:getParts" />
<output message="tns:getPartsResponse" />
</operation>
<operation name="postOrder"
parameterOrder="siteId secretKey language customerName emailAddress deliveryAddress billingAddress creditCard cvv orderLines shippingCharge reference poNumber text">
<input message="tns:postOrder" />
<output message="tns:postOrderResponse" />
</operation>
</portType>
<binding name="RetailOrderServerPortBinding" type="tns:RetailOrderServer">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http"
style="rpc" />
<operation name="getParts">
<soap:operation soapAction="" />
<input>
<soap:body use="literal" namespace="http://ws.reliableparts.net/" />
</input>
<output>
<soap:body use="literal" namespace="http://ws.reliableparts.net/" />
</output>
</operation>
<operation name="postOrder">
<soap:operation soapAction="" />
<input>
<soap:body use="literal" namespace="http://ws.reliableparts.net/" />
</input>
<output>
<soap:body use="literal" namespace="http://ws.reliableparts.net/" />
</output>
</operation>
</binding>
<service name="RetailOrderServerService">
<port name="RetailOrderServerPort" binding="tns:RetailOrderServerPortBinding">
<soap:address location="http://www.reliableparts.net/dataservicetc/retail" />
</port>
</service>
</definitions>
以下是使用$ client-&gt; __ getLastRequest()
的结果xmlns:ns1="http://ws.reliableparts.net/">
<SOAP-ENV:Body>
<ns1:postOrder>
<siteId>REDACTED</siteId>
<secretKey>REDACTED</secretKey>
<language>en</language>
<customerName>Eric Dodson</customerName>
<emailAddress>dodson@dodson.com</emailAddress>
<deliveryAddress id="ref1">
<addressLine1>891 Prairie Grass Drive</addressLine1>
<addressLine2 />
<addressLine3 />
<city>Las Vegas</city>
<countryCode>1</countryCode>
<name>Eric Dodson</name>
<postalCode>89123</postalCode>
<stateCode>NV</stateCode>
</deliveryAddress>
<deliveryAddress href="#ref1" />
<creditCard>
<expirationDate>201605</expirationDate>
<number>4111111111111111</number>
<type>Visa</type>
</creditCard>
<cvv>123</cvv>
<orderLines>
<item>
<manufacturerCode>RPL</manufacturerCode>
<partNumber>341241</partNumber>
<price>$99.99</price>
<quantity>1</quantity>
<text>No Comments</text>
</item>
</orderLines>
<shippingCharge>0</shippingCharge>
<reference>string</reference>
<poNumber>string</poNumber>
<text>string</text>
</ns1:postOrder>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>