0

看了其他一些关于此的 SO 帖子,但没有任何乐趣。

我有这个代码:

$url = "http://itunes.apple.com/us/rss/toppaidapplications/limit=10/genre=6014/xml";
$string = file_get_contents($url);
$string = preg_replace("/(<\/?)(\w+):([^>]*>)/", "$1$2$3", $string);
$xml = simplexml_load_string($string);

foreach ($xml->entry as $val) {
    echo "RESULTS: " . $val->attributes() . "\n";

但我无法得到任何结果。我特别有兴趣在此片段中获取 ID 值,即 549592189:

<id im:id="549592189" im:bundleId="com.activision.wipeout">http://itunes.apple.com/us/app/wipeout/id549592189?mt=8&amp;uo=2</id>

有什么建议么?

4

3 回答 3

0

尝试xpath

$doc     = new DOMDocument;
@$doc->loadHTML($string);
$xpath   = new DOMXpath($doc);
$r       = $xpath->query("//id/@im:id");
$id      = $r->item(0)->value;
于 2012-09-07T22:05:14.177 回答
0

SimpleXML 为您提供了深入研究 XML 结构并获取所需元素的简单方法。不需要正则表达式,不管它做什么。

<?php

// Load XML
$url = "http://itunes.apple.com/us/rss/toppaidapplications/limit=10/genre=6014/xml";
$string = file_get_contents($url);
$xml = new SimpleXMLElement($string);

// Get the entries
$entries = $xml->entry;

foreach($entries as $e){
    // Get each entriy's id
    $id = $e->id;
    // Get the attributes
    // ID is in the "im" namespace
    $attr = $id->attributes('im', TRUE);
    // echo id
    echo $attr['id'].'<br/>';
}

演示:http ://codepad.viper-7.com/qNo7gs

于 2012-09-07T22:11:57.880 回答
0

尝试:

$sxml = new SimpleXMLElement($url);
for($i = 0;$i <=10;$i++){
$appid= $sxml->entry[$i]->id->attributes("im",TRUE);
echo $appid;
}
于 2013-09-04T19:43:58.617 回答