Search code examples
phpsoap-client

SOAP Result returns negative number but does not recognize by IF ELSE


I have here a SOAP Web Service request here that returns a negative value but when I used it on an IF ELSE method It does not recognize and the condition always go to ELSE even if the value is equal. Please see code below:

$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

Solution

  • I have found a solution. I just convert the value to SimpleXMLElement then encode it to json using json_encode then decode it using json_decode then get the json element

    $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