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
PHPXML

Avatar of undefined
Last Comment
Richard Lloyd

8/22/2022 - Mon
ste5an

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

Take a look at SimpleXML.
ASKER CERTIFIED SOLUTION
Richard Lloyd

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Julian Hansen

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

Richard Lloyd

ASKER
Julian

Thanks. Appreciate your input as normal.
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23