2

我像这样使用 SimpleXMLElement:

$visitor_body = wp_remote_retrieve_body( $url );
$visitor_data = new SimpleXMLElement( $visitor_body );

我有以下 XML 结构响应:

<resultSet>
  <aggregates>
     <metric name="visitDuration" value="488" label="Avg. Visit Duration"/>
     <metric name="uniqueVisitors" value="8" label="Unique Visitors"/>
     <metric name="visits" value="14" label="Visits"/>
     <metric name="pageViews" value="35" label="Page Views"/>
     <metric name="bounceRate" value="0.5" label="Bounce Rate"/>
     <metric name="pagesPerVisit" value="2.5" label="Pages Per Visit"/>
  </aggregates>

我需要以我可以在不同地方使用的方式获得visitDuration uniqueVisitors visits等等的价值。例如,在一个变量中,例如:

echo $visitDuration;
echo $uniqueVisitors;
4

1 回答 1

1

http://php.net/manual/en/book.simplexml.php - 这个页面是你需要研究的地方。

或者这个怎​​么样?

http://simplehtmldom.sourceforge.net/

* A HTML DOM parser written in PHP5+ let you manipulate HTML in a very easy way!
* Require PHP 5+.
* Supports invalid HTML.
* Find tags on an HTML page with selectors just like jQuery.
* Extract contents from HTML in a single line.

// Find all visitDuration
foreach($html->find('visitDuration') as $element)
       echo $element->value. '<br>'; // returns 488

// Find all bounceRate
foreach($html->find('bounceRate') as $element)
       echo $element->value. '<br>'; // returns 0.5
于 2012-11-24T05:24:34.737 回答