0

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) ?
1
  • Use simplexml_load_string(). Commented Mar 1, 2014 at 6:53

1 Answer 1

1

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
)
Sign up to request clarification or add additional context in comments.

1 Comment

Awesome! This fixed the problem. Thank you for the prompt response! This makes perfect sense. I'm not sure why I couldn't find anything else that would work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.