首页 > 解决方案 > SOAP 结果返回负数,但 IF ELSE 无法识别

问题描述

我在这里有一个 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

标签: phpsoap-client

解决方案


我找到了解决方案。我只是将值转换为SimpleXMLElement然后使用将其编码为jsonjson_encode然后使用解码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

推荐阅读