【问题标题】:SOAP Result returns negative number but does not recognize by IF ELSESOAP 结果返回负数,但 IF ELSE 无法识别
【发布时间】:2021-04-06 02:28:28
【问题描述】:

我在这里有一个 SOAP Web 服务请求,它返回一个负值,但是当我在 IF ELSE 方法上使用它时,它无法识别并且即使值相等,条件也总是转到 ELSE。请看下面的代码:

$url = 'http://0.0.0.0/webservice/SampleWebservice.asmx?WSDL';
$context = stream_context_create(array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
)
));
$rq = ["Value1" => "value",
"ClientKey" => "ABCDEFGHIJKLMNOPQRSTUVWXYZ123456789"];
$service = new SoapClient($url, array('stream_context' => $context));
$xml = $service->GetWebServiceRequestVariable($rq);
$array = json_decode(json_encode($xml), true);
$value = $array['GetWebServiceRequestVariable_Result']['any'];
//value returns -1
if($value == -1) {
 echo 'Value is negative';
}
else {
echo 'Value is positive';
}
//returns Value is Positive

【问题讨论】:

  • 您能否尝试在 if 语句之前打印出$value,同时使用var_dump($value); 检查数据类型另外,它是否总是严格具有-1 或值> 0
  • 它返回<diffgr:diffgram xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1"><NewDataSet xmlns=""><Table diffgr:id="Table1" msdata:rowOrder="0"><Value>-1</Value></Table></NewDataSet></diffgr:diffgram>
  • 是上面的返回值在变量$value里面吗?
  • soap 请求在进入 IF ELSE 时返回 -1,-1 无法识别并始终进入 else 部分。
  • 我可能遗漏了一些东西,但是当我尝试使用 $array = json_decode(json_encode('<diffgr:diffgram xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1"><NewDataSet xmlns=""><Table diffgr:id="Table1" msdata:rowOrder="0"><Value>-1</Value></Table></NewDataSet></diffgr:diffgram>'), true); 运行您的部分代码时。我得到echo $array-1

标签: php soap-client


【解决方案1】:

我找到了解决办法。我只是将值转换为SimpleXMLElement,然后使用json_encode将其编码为json,然后使用json_decode对其进行解码,然后获取json元素

$url = 'http://0.0.0.0/webservice/SampleWebservice.asmx?WSDL';
$context = stream_context_create(array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
)
));

$rq = ["Value1" => "value",
"ClientKey" => "ABCDEFGHIJKLMNOPQRSTUVWXYZ123456789"];
$service = new SoapClient($url, array('stream_context' => $context));
$xml = $service->GetWebServiceRequestVariable($rq);
$array = json_decode(json_encode($xml), true);

$xmljson = new SimpleXMLElement($array['GetWebServiceRequestVariable_Result']['any']);
$cvjson = json_encode($xmljson);
$bcjson = json_decode($cvjson,true);
$value = $bcjson["NewDataSet"]["Table"]["Value"];

if($value == -1) {
 echo 'Value is negative';
}
else {
echo 'Value is positive';
}
//now returns Value is negative

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-07-20
    • 1970-01-01
    • 2020-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-13
    相关资源
    最近更新 更多