dtcd27183 2012-06-28 18:21
浏览 112
已采纳

php soapclient返回null但getPreviousResults有正确的结果

I've ran into trouble with SOAP, I've never had this issue before and can't find any information on line that helps me solve it.

The following code

$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.

I've tried passing params into the SoapClient constructor to define soap version but that didnt' help. I've also tried it with the trace param set to false and not present which as expected made $response null but $res was still null. I've tried the code on both a linux and windows install running Apache.

The function definition in the WSDL is (xxxx is for security reasons)

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

I have it working using the __getLastResponse() but its annoying me it will not work properly.

I've put together a small testing script, does anyone see any issues here. Do I need a structure for the return type?

//very simplifed dataset that would normally be 
//read in from a CSV file of about 1mb
$soapInput = getSoapInput("asdf,qwer
zzxvc,ewrwe
23424,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("
", $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;

}
  • 写回答

2条回答 默认 最新

  • dongwopu8210 2012-06-29 19:45
    关注

    Ok after much digging it is related to relative namespaces, it appears that PHP doesn't handle them well within the WSDL or the SOAP Envelope. So since I don't have control of the SOAP Server I will continue to get the response via __getLastResponse();.

    Hopefully this will save some people some time it was hard to find.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?