php - 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
解决方案
我找到了解决方案。我只是将值转换为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
推荐阅读
- java - 编译器在解析的 XML 文件中省略了 DOCTYPE,如何将其包含在最终文件中?
- angular - 使用 Karma / Jasmine 进行 Angular 8 测试 -> 角度路径中 loadChildren 未涵盖 100% 代码覆盖率
- javascript - Gauge charts with custom ranges
- c# - EFCore save entity with relation ids
- python-3.x - 为什么在字典中当我们有相同的值和不同的键名时为什么它选择一个与另一个相比
- system - 以客户为中心的一致性的实际例子
- laravel-5 - 如果记录不存在则插入,如果存在则更新
- c# - Unity3d Photon custom room property problem for making countdown timer
- c# - 为什么 WMI 的结果在标准模式和 Windows 7 中的管理员用户中不同?
- android - 将 MutableLiveData 公开为 LiveData 的正确方法?