Reputation: 89
I am interacting with Soap Web services and I would want to read the values from the soap object. I am getting an exception all the time. See below;
Sample soap;
<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
<c2b:C2BPaymentValidationRequest xmlns:c2b="http://cps.huawei.com/cpsinterface/c2bpayment">
<TransID>3IN40005PY</TransID>
<TransTime>20160923143639</TransTime>
<TransAmount>3000</TransAmount>
<BusinessShortCode>10069</BusinessShortCode>
<MSISDN>86812530102</MSISDN>
<KYCInfo>
<KYCName>FirstName</KYCName>
<KYCValue>aaa</KYCValue>
</KYCInfo>
<KYCInfo>
<KYCName>MiddleName</KYCName>
<KYCValue>bbb</KYCValue>
</KYCInfo>
<KYCInfo>
<KYCName>LastName</KYCName>
<KYCValue>ccc</KYCValue>
</KYCInfo>
</c2b:C2BPaymentValidationRequest>
</soapenv:Body>
</soapenv:Envelope>
I tried making an object
$xml=simplexml_load_string($response) or die("Error: Cannot create object");
So that I can read the values as below;
$TransID=$xml->TransID;
$TransTime=$xml->TransTime;
$TransAmount=$xml->TransAmount;
But it fails here die("Error: Cannot create object");
Or even better how can I change the soap request to json and get the values? Anyone?
Upvotes: 0
Views: 223
Reputation: 666
It seems namespace declaration (php manual) for SOAP is missing in your code.
$xml = simplexml_load_string($xml_data);
$xml->registerXPathNamespace('c2b', 'http://cps.huawei.com/cpsinterface/c2bpayment');
$response = $xml->xpath('//c2b:C2BPaymentValidationRequest') or die("Error: Cannot create object");
print_r($response);
Output:
Array
(
[0] => SimpleXMLElement Object
(
[TransID] => 3IN40005PY
[TransTime] => 20160923143639
[TransAmount] => 3000
[BusinessShortCode] => 10069
[MSISDN] => 86812530102
[KYCInfo] => Array
(
[0] => SimpleXMLElement Object
(
[KYCName] => FirstName
[KYCValue] => aaa
)
[1] => SimpleXMLElement Object
(
[KYCName] => MiddleName
[KYCValue] => bbb
)
[2] => SimpleXMLElement Object
(
[KYCName] => LastName
[KYCValue] => ccc
)
)
)
)
Upvotes: 1