Reputation: 97
I have searched here and the rest of the net and I can't find any help with this.
I have a response from a curl_exec. The response is an XML string called $output.
echo '<pre>' . htmlentities($output) . '</pre>';
Gives:
<?xml version="1.0" encoding="utf-8"?>
<RESPONSE>
<FIELDS>
<FIELD KEY="status">1</FIELD>
<FIELD KEY="auth_code">DEMO43</FIELD>
<FIELD KEY="auth_response">APPROVED</FIELD>
<FIELD KEY="avs_code">X</FIELD>
<FIELD KEY="cvv2_code"> </FIELD>
<FIELD KEY="order_id">12345678900015</FIELD>
<FIELD KEY="reference_number">47210</FIELD>
<FIELD KEY="error" />
<FIELD KEY="available_balance" />
<FIELD KEY="is_partial">0</FIELD>
<FIELD KEY="partial_amount">0</FIELD>
<FIELD KEY="partial_id" />
<FIELD KEY="original_full_amount" />
<FIELD KEY="outstanding_balance">0</FIELD>
</FIELDS>
</RESPONSE>
I need to pull out specific information, for example, the value associated with "auth_code" (ie: DEMO43).
After many hours of fruitless searching, I need help. How do I get this data into an associative array like:
$array = ('status'=>'1','auth_code'=>'DEMO43' ... etc) ?
Upvotes: 0
Views: 36
Reputation: 9635
try this
$string = '<?xml version="1.0" encoding="utf-8"?>
<RESPONSE>
<FIELDS>
<FIELD KEY="status">1</FIELD>
<FIELD KEY="auth_code">DEMO43</FIELD>
<FIELD KEY="auth_response">APPROVED</FIELD>
<FIELD KEY="avs_code">X</FIELD>
<FIELD KEY="cvv2_code"> </FIELD>
<FIELD KEY="order_id">12345678900015</FIELD>
<FIELD KEY="reference_number">47210</FIELD>
<FIELD KEY="error" />
<FIELD KEY="available_balance" />
<FIELD KEY="is_partial">0</FIELD>
<FIELD KEY="partial_amount">0</FIELD>
<FIELD KEY="partial_id" />
<FIELD KEY="original_full_amount" />
<FIELD KEY="outstanding_balance">0</FIELD>
</FIELDS>
</RESPONSE>';
$ARR_OUTPUT = array();
$dom = new DOMDocument();
$dom->loadXML($string);
$searchNode = $dom->getElementsByTagName( "FIELD" );
foreach( $searchNode as $searchNode )
{
$key = $searchNode->getAttribute('KEY');
$value = $searchNode->nodeValue;
$ARR_OUTPUT[$key]=$value;
}
echo "<pre>";
print_r($ARR_OUTPUT);
OUTPUT :
Array
(
[status] => 1
[auth_code] => DEMO43
[auth_response] => APPROVED
[avs_code] => X
[cvv2_code] =>
[order_id] => 12345678900015
[reference_number] => 47210
[error] =>
[available_balance] =>
[is_partial] => 0
[partial_amount] => 0
[partial_id] =>
[original_full_amount] =>
[outstanding_balance] => 0
)
Upvotes: 1