We help IT Professionals succeed at work.

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

Richard Lloyd
on
77 Views
Last Modified: 2019-03-21
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

ste5anSenior Developer
CERTIFIED EXPERT

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

Take a look at SimpleXML.
This problem has been solved!
(Unlock this solution with a 7-day Free Trial)
UNLOCK SOLUTION
CERTIFIED EXPERT
Most Valuable Expert 2017
Distinguished Expert 2019

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.