Link to home
Start Free TrialLog in
Avatar of Richard Lloyd
Richard Lloyd

asked on

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

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
Avatar of ste5an
ste5an
Flag of Germany image

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

Take a look at SimpleXML.
ASKER CERTIFIED SOLUTION
Avatar of Richard Lloyd
Richard Lloyd

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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

Avatar of Richard Lloyd
Richard Lloyd

ASKER

Julian

Thanks. Appreciate your input as normal.