带有curl的php解析器xml,仅显示我想要的标签[重复]

Posted

技术标签:

【中文标题】带有curl的php解析器xml,仅显示我想要的标签[重复]【英文标题】:php parser xml with curl, display only tags that i want [duplicate] 【发布时间】:2012-10-01 14:41:27 【问题描述】:

可能重复: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。我不希望页面上显示其他结果。我该怎么做呢?

【问题讨论】:

您可以通过解析 XML 来做到这一点,请参阅链接以了解存在的许多选项。 【参考方案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

【讨论】:

以上是关于带有curl的php解析器xml,仅显示我想要的标签[重复]的主要内容,如果未能解决你的问题,请参考以下文章

无效格式xml的最佳PHP解析器[重复]

PHP:JSON 或 XML 解析器更快吗?

php解析xml,并将xml转换为层级数组

PHP cURL,提取 XML 响应

是否有用于 PHP 的宽松、宽松的 XML 解析器?

如何使用 PHP 解析带有冒号标签的 XML 节点