問題描述
伙計們,我被困住了,在過去的幾個小時里我的頭從桌子上摔下來.
Guys, I'm stuck, banging my head off the desk for the past few hours.
我正在嘗試使用一項服務(wù),并且我調(diào)用了其他 8 個函數(shù),它們本質(zhì)上與這個函數(shù)幾乎相同,但是這個函數(shù)會導(dǎo)致SOAP-ERROR: Encoding: Violation of encoding rules"錯誤.
I am trying to consume a service, and I have 8 other functions that I call that are almost IDENTICAL in nature to this one, but this one, results in a 'SOAP-ERROR: Encoding: Violation of encoding rules' error.
這里是函數(shù)調(diào)用(為安全起見省略了 wsdl):
Heres the function call (wsdl omitted for security):
function CanLoadProduct($data){
$client = new SoapClient('wsdl-url');
$params = array('username' => $this->username,
'password' => $this->password,
'prod' => $data['productid'],
'mdn' => $data['mdn']);
try {
$reply = $client->__soapCall("CanLoadProduct", $params);
} catch (Exception $e) {
echo 'Error: ', $e->getMessage(), "
";
print_r($params);
die();
}
if( $reply['result'] == 1 ){
return TRUE; // 1 = true
} else {
return FALSE;
}
}
好的,這個函數(shù),連接到一個webservice,需要的元素是:用戶名、密碼、prod、mdn,我將所有這 4 個作為 $params 數(shù)組的一部分提供.用戶名/通行證定義較早,并且工作正常,因為其他 8 個函數(shù)使用 Web 服務(wù)沒有任何問題.
Ok so this function, connects to a webservice, the required elements are: username, password, prod, mdn, all 4 of which I supply as part of the $params array. Username/Pass are defined earlier, and do work fine, as the other 8 functions consume the web service without any problems.
$data[] 數(shù)組(我傳遞給函數(shù)的)包含:$data['productid']$data['mdn']沒有使用其他任何東西.
The $data[] array (that I pass to the function), contains: $data['productid'] $data['mdn'] nothing else is used.
我得到了
SOAP-ERROR: Encoding: Violation of encoding rules
出于某種無法解釋的原因,谷歌搜索這個錯誤讓我無處可去.還有人遇到這個嗎?運行 PHP 5.2.9-2.奇怪的是,這與這個 100% 有效的函數(shù)相同:
for some unexplained reason, and Googling this error gets me nowhere. Anyone else run into this? Running PHP 5.2.9-2. The strange thing is this is identical to this function which works 100%:
function GetPIN($productid){
$client = new SoapClient('wsdl-url');
$params = array('username' => $this->username,
'password' => $this->password,
'prod' => $productid);
try {
$reply = $client->__soapCall("GetPIN", $params);
} catch (Exception $e) {
echo 'Error: ', $e->getMessage(), "
";
die();
}
return $reply;
}
這是 WSDL(應(yīng)該先發(fā)布這個):
Here is the WSDL (should have posted this first):
<?xml version="1.0" encoding="ISO-8859-1"?>
<definitions xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:tns="ready:test" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="ready:test">
<types>
<xsd:schema targetNamespace="ready:test"
>
<xsd:import namespace="http://schemas.xmlsoap.org/soap/encoding/" />
<xsd:import namespace="http://schemas.xmlsoap.org/wsdl/" />
</xsd:schema>
</types>
<message name="CanLoadProductRequest">
<part name="username" type="xsd:string" />
<part name="password" type="xsd:string" />
<part name="prod" type="xsd:string" />
<part name="mdn" type="xsd:string" />
<part name="esn" type="xsd:string" /></message>
<message name="CanLoadProductResponse">
<part name="result" type="xsd:int" /></message>
<portType name="CanLoadProductPortType">
<operation name="CanLoadProduct">
<input message="tns:CanLoadProductRequest"/>
<output message="tns:CanLoadProductResponse"/>
</operation>
</portType>
<binding name="CanLoadProductBinding" type="tns:CanLoadProductPortType">
<soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/>
<operation name="CanLoadProduct">
<soap:operation soapAction="{url-removed}" style="rpc"/>
<input>
<soap:body use="encoded" namespace=""
encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
</input>
<output>
<soap:body use="encoded" namespace=""
encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
</output>
</operation>
</binding>
<service name="CanLoadProduct">
<port name="CanLoadProductPort" binding="tns:CanLoadProductBinding">
<soap:address location="{url-removed}"/>
</port>
</service>
</definitions>
推薦答案
看起來您的某處類型不匹配,要么在組裝請求時(其中一個參數(shù)不是字符串類型),要么服務(wù)器返回其他內(nèi)容而不是 int(違反 WSDL 響應(yīng)定義,從而導(dǎo)致客戶端認為響應(yīng)無效,因為它期望其他內(nèi)容).
It looks like you have a type mismatch somewhere, either while assembling your request (one of the parameters is not of type string), or the server returns something other than an int (violating the WSDL response definition and thus causing the client to consider the response invalid, as it expects something else).
- 要測試第一種情況,請確保首先將所有參數(shù)轉(zhuǎn)換為字符串
- 要測試第二種情況,請創(chuàng)建您的 SoapClient,并將 trace 選項設(shè)置為 true,以便之后通過 $client->__getLastResponse() 從服務(wù)器獲得對實際 XML 答案的訪問(您也可以通過 __getLastRequest() 將其用于請求調(diào)試.
- To test the first case, ensure casting all parameters to string first
- To test the second case, create your SoapClient with the trace option set to true in order to gain access to the actual XML answer from the server via $client->__getLastResponse() afterwards (You can use this for request debugging also via __getLastRequest()).
一些額外的觀察/問題:
Some additional observations/questions:
- 根據(jù)已發(fā)布的 WSDL,CanLoadProductRequest"具有第五個參數(shù)esn",您不會在函數(shù)調(diào)用中提供該參數(shù).
- 您使用
$client->__soapCall("CanLoadProduct", $params)
而不是$client->CanLoadProduct($username, $password, etc.) 的任何原因代碼>?(第一個版本是一個較低級別的變體,旨在用于非 WSDL 場景.第二個版本可能會為您提供更詳細的錯誤/異常)
- 您能否通過其他方式測試對 CanLoadProductRequest 的 SOAP 調(diào)用?錯誤可能在服務(wù)器端,試圖返回不符合 WSDL 定義的結(jié)果類型.
- According to the posted WSDL, the 'CanLoadProductRequest' has a fifth param 'esn', which you do not supply in your function call.
- Any reason why you use
$client->__soapCall("CanLoadProduct", $params)
instead of$client->CanLoadProduct($username, $password, etc.)
? (The first version is a lower level variation which is intended to be used for non_WSDL scenarios. The second version might give you a more detailed error/exception) - Can you test the SOAP Call to CanLoadProductRequest by some other means? The error could be on the server side, trying to return a result type that does not fit the WSDL definition.
這篇關(guān)于SOAP-ERROR:編碼:違反編碼規(guī)則?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!