我在这里和网络的其他部分进行了搜索,我找不到任何帮助。
我收到了curl_exec的回复。响应是一个名为$ output的XML字符串。
echo '<pre>' . htmlentities($output) . '</pre>';
给出:
<?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>
我需要提取具体信息,例如与“auth_code”相关联的值(即:DEMO43)。
经过几个小时徒劳无功的搜索,我需要帮助。如何将此数据转换为关联数组,如:
$array = ('status'=>'1','auth_code'=>'DEMO43' ... etc) ?
答案 0 :(得分:1)
试试这个
$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);
输出:
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
)