How to get the pinBlocked tag from the following SOAP XML response in PHP
This is my SOAP XML Response:
<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><ReadCardStatusResponse xmlns="http://theapi.com"><result>1</result><errorMessage>ok</errorMessage><status><terminalID>123456789</terminalID><profileNumber>123456789</profileNumber><reference>37292141</reference><valid>true</valid><pinBlocked>true</pinBlocked><activated>true</activated><retired>false</retired><loaded>true</loaded><redeemed>true</redeemed><empty>true</empty><cancelled>false</cancelled><stopped>true</stopped><lost>false</lost><stolen>false</stolen><expired>false</expired><transactionID>blahblah</transactionID><transactionDate>2004-10-28T08:54:27</transactionDate><checksum>blahblah</checksum><resultCode>1</resultCode><resultText>ok</resultText></status></ReadCardStatusResponse></soap:Body></soap:Envelope>
the response is saved in $result
i tried
$result->pinBlocked;
CodePudding user response:
You need to create object of SimpleXMLElement
with your xml as string, then register namespace for http://theapi.com
using random prefix (in this case I'm using a
) and then, using xpath
get your element with previously registered prefix.
$xml = new SimpleXMLElement($result);
$xml->registerXPathNamespace('a', 'http://theapi.com');
echo (bool)$xml->xpath('//a:pinBlocked')[0];
For PHP 5.3.3 use this:
$xml = new SimpleXMLElement($result);
$xml->registerXPathNamespace('a', 'http://theapi.com');
$item = $xml->xpath('//a:pinBlocked');
echo $item[0];