2012-06-28 34 views
1

我遇到過使用SOAP的麻煩,我從來沒有遇到過這個問題,也找不到任何幫助我解決問題的信息。php soapclient返回null,但getPreviousResults有正確的結果

下面的代碼

$wsdl = "path/to/my/wsdl"; 
$client = new SoapClient($wsdl, array('trace' => true)); 
//$$textinput is passed in and is a very large string with rows in <item></item> tags 
$soapInput = new SoapVar($textinput, XSD_ANYXML); 
$res = $client->dataprofilingservice(array("contents" => $soapInput)); 
$response = $client->__getLastResponse(); 

var_dump($res);//outputs null 
var_dump($response);//provides the proper response as I would expect. 

我已經試過路過PARAMS到SoapClient的構造函數中定義的肥皂版本但didnt的幫助。我也試過跟蹤參數設置爲false,不存在,正如預期的那樣,$ response null爲空,但$ res仍然爲空。我已經嘗試了運行Apache的Linux和Windows安裝上的代碼。

在WSDL函數定義爲(xxxx是出於安全原因)

<portType name="xxxxServiceSoap"> 
<operation name="dataprofilingservice"> 
    <input message="tns:dataprofilingserviceSoapIn"/> 
    <output message="tns:dataprofilingserviceSoapOut"/> 
</operation> 
</portType> 

我有工作使用__getLastResponse(),但它的討厭我,這將無法正常工作。

我已經放在一起一個小測試腳本,有沒有人看到這裏的任何問題。我是否需要返回類型的結構?

//very simplifed dataset that would normally be 
//read in from a CSV file of about 1mb 
$soapInput = getSoapInput("asdf,qwer\r\nzzxvc,ewrwe\r\n23424,2113"); 
$wsdl = "path to wsdl"; 

try { 
    $client = new SoapClient($wsdl,array('trace' => true,'exceptions' => true)); 

} catch (SoapFault $fault) { 
    $error = 1; 
    var_dump($fault); 
} 

try { 
    $res = $client->dataprofilingservice(array("contents" => $soapInput)); 
    $response = $client->__getLastResponse(); 
    echo htmlentities($client->__getLastRequest());//proper request echoed 
    echo '<hr>'; 
    var_dump($res);//null 

    echo "<hr>"; 
    echo(htmlentities($response));//proper response echoed 
} catch (SoapFault $fault) { 
    $error = 1; 
    var_dump($fault); 

}   
function getSoapInput($input){ 
$rows = array(); 
    $userInputs = explode("\r\n", $input); 

    $userInputs = array_filter($userInputs); 
    // 
    $inputTemplate = " <contents>%s</contents>"; 
    $rowTemplate = "<Item>%s</Item>"; 
    // 
    $soapString = ""; 
    foreach ($userInputs as $row) { 
     // sanitize 
     $row = htmlspecialchars(addslashes($row)); 
     $xmlStr = sprintf($rowTemplate, $row); 
     $rows[] = $xmlStr; 
    } 
    $textinput = sprintf($inputTemplate, implode(PHP_EOL, $rows)); 
    $soapInput = new SoapVar($textinput, XSD_ANYXML); 

    return $soapInput; 

} 

回答

1

經過多次挖掘,它與相對命名空間有關,看起來PHP並沒有在WSDL或SOAP Envelope中很好地處理它們。因此,由於我沒有控制SOAP服務器,我將繼續通過__getLastResponse();獲得響應。

希望這會節省一些人一段時間很難找到。

0

您在這裏混合的東西。 __getLastResponse()如果您使用'trace' => true選項,則會從服務器返回裸露的XML字符串響應。這僅用於調試。

因此,無論'trace' => true與否,您最初想要調用的方法都會返回相同的結果,這是完全正常的。將跟蹤設置爲開不會改變行爲,它只是提供了一個附加功能,返回值爲__getLastResponse()

由於SoapClient不會拋出任何異常我會說你的電話會好起來的,NULL是一個有效的返回值。

可能想要提供實際的XML字符串和/或WSDL定義,以便人們可以檢查是否是這種情況。

+0

感謝您的回覆 - 不幸的是,我無法提供整個WSDL /代碼。我知道getLastResponse是用於調試的,但是如果它提供的是響應的裸XML而不是響應。由於唯一的soap函數調用是$ res = $ client-> dataprofilingservice(array(「contents」=> $ soapInput));這就是產生「最後的迴應」的原因。 –