How to get variables out of an XML response to a curl post

Richard Lloyd
Richard Lloyd used Ask the Experts™
on
I need some advice on getting variables out of an XML response to a curl post.

The curl produces an xml response, eg:

<?xml version="1.0"?><ncresponse
orderID="TEST-21"
PAYID="304713xyz72"
PAYIDSUB="0"
NCSTATUS="0"
NCERROR="0"
ACCEPTANCE="test123"
STATUS="9"
IPCTY="99"
CCCTY="BE"
ECI="7"
CVCCheck="NO"
AAVCheck="NO"
VC="NO"
AAVZIP="NO"
AAVADDRESS="NO"
AAVNAME="NO"
AAVPHONE="NO"
AAVMAIL="NO"
amount="12.34"
currency="GBP"
PM="CreditCard"
BRAND="VISA"
TICKET="No Transaction found"
SUBBRAND="TEST"
ALIAS="010089D0-B4F9-876S-AA4E-14F807CB1889"
ECOM_BILLTO_POSTAL_CITY=""
ECOM_BILLTO_POSTAL_COUNTRYCODE=""
ECOM_BILLTO_POSTAL_STATEDESC=""
ECOM_BILLTO_POSTAL_STREET_LINE1=""
ECOM_BILLTO_POSTAL_STREET_LINE2=""
ECOM_BILLTO_POSTAL_POSTALCODE=""
CN="111"
ECOM_BILLTO_POSTAL_NAME_LAST=""
ECOM_BILLTO_POSTAL_NAME_FIRST="111"
ECOM_BILLTO_TELECOM_PHONE_NUMBER=""
ECOM_SHIPTO_POSTAL_CITY=""
ECOM_SHIPTO_POSTAL_COUNTRYCODE=""
ECOM_SHIPTO_POSTAL_STATEDESC=""
ECOM_SHIPTO_POSTAL_STREET_LINE1=""
ECOM_SHIPTO_POSTAL_STREET_LINE2=""
ECOM_SHIPTO_POSTAL_POSTALCODE=""
ECOM_SHIPTO_POSTAL_NAME_LAST="111"
NCERRORPLUS="!"
BIN="411111">
</ncresponse>

Open in new window


How do I extract each of the variables to use for further processing with PHP
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
ste5anSenior Developer

Commented:
There is no variable.. only one element with many attributes.

Take a look at SimpleXML.
I have managed to find an answer to my own question:

$connection = curl_init('https://xxxxxx.com');
curl_setopt ($connection, CURLOPT_CONNECTTIMEOUT, 20);
curl_setopt ($connection, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($connection, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt ($connection, CURLOPT_USERAGENT,$_SERVER['HTTP_USER_AGENT']);
curl_setopt ($connection, CURLOPT_POSTFIELDS,$actual_string);
$result = curl_exec($connection);
curl_close($connection);

$xml   = simplexml_load_string($result);
$array = json_decode(json_encode((array) $xml), true);
$array = array($xml->getName() => $array);
var_dump($array)

Open in new window

Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
Why go through the json_decode / json_encode process? simplexml_load_string returns a SimpleXMLElement object you can use to get the data out with its own functions.
$connection = curl_init('https://xxxxxx.com');
curl_setopt ($connection, CURLOPT_CONNECTTIMEOUT, 20);
curl_setopt ($connection, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($connection, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt ($connection, CURLOPT_USERAGENT,$_SERVER['HTTP_USER_AGENT']);
curl_setopt ($connection, CURLOPT_POSTFIELDS,$actual_string);
$result = curl_exec($connection);
curl_close($connection);

$xml = simplexml_load_string($result);
foreach($xml->attributes() as $k => $v) {
	echo "Attribute: [{$k}] : {$v}<br>";
}

Open in new window

Author

Commented:
Julian

Thanks. Appreciate your input as normal.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial