可能重复:
Best XML Parser for PHP
我有一个Php / curl文件
<?php
echo "Conversion Stats<br />";
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_VERBOSE, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible;)");
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_URL,'http://login.leadkitchen.com/affiliates/api/4/reports.asmx/Conversions' );
$post_array = array(
'affiliate_id' => '123',
'api_key' => 'abc',
'start_date' => '10/8/2012',
'end_date' => '10/11/2012',
'offer_id' => '123',
'start_at_row' => '1',
'row_limit' => '0',
'sub_affiliate' => 'email'
);
//url-ify the data
foreach($post_array as $key=>$value)
{
$post_array_string .= $key.'='.$value.'&';
}
$post_array_string = rtrim($post_array_string,'&');
//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_POST,count($post_array ));
curl_setopt($ch,CURLOPT_POSTFIELDS,$post_array_string);
$response = curl_exec($ch);
print "$response";
?>
此可能的XML响应
<conversion_response>
<success>true</success>
<row_count>1</row_count>
<summary>
<price>22.26555000000000</price>
<currency_symbol>$</currency_symbol>
</summary>
<conversions>
<conversion>
<conversion_id>221D4853</conversion_id>
<conversion_date>2011-06-24T11:51:05.117</conversion_date>
<offer_id>100</offer_id>
<offer_name>$100 Bendy's Gift Card + Free Frosti</offer_name>
<campaign_id>1781</campaign_id>
<subid_1>111770</subid_1>
<subid_2/>
<subid_3/>
<subid_4/>
<subid_5/>
<price>15.0000</price>
<disposition>Voicemail matches name</disposition>
<test>false</test>
<currency_symbol>€</currency_symbol>
</conversion>
</conversions>
</conversion_response>
我实际上只需要在页面中显示conversion_id和price。我不希望在页面上显示其他结果。我该怎么做呢?
答案 0 :(得分:1)
您可以使用SimpleXMLElement
获取所需的元素
$xml = new SimpleXMLElement($response);
echo "<pre>";
echo $xml->conversions->conversion->conversion_id, PHP_EOL;
echo $xml->conversions->conversion->price, PHP_EOL;
输出
221D4853
15.0000