0

Looked at a few other SO posts on this but no joy.

I've got this code:

$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";

but I can't get any results. I'm specifically interested in getting the ID value which would be 549592189 in this fragment:

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

Any suggestions?

Snowcrash
  • 80,579
  • 89
  • 266
  • 376
  • Don't manipulate html/xml with regexes. you're just begging for trouble. – Marc B Sep 07 '12 at 21:52
  • You should read [Parsing Html The Cthulhu Way](http://www.codinghorror.com/blog/2009/11/parsing-html-the-cthulhu-way.html). It's pertaining to html, but xml applies as well. – ShadowScripter Sep 07 '12 at 21:55
  • Re regex, perhaps this has been solved but see: http://www.php.net/manual/en/simplexmlelement.construct.php#94709 – Snowcrash Sep 10 '12 at 08:50
  • @SnowCrash: Don't always trust the comments on PHP.net, they are posted by users, and may not be correct. In this case, that comment is wrong. SimpleXML works fine with XML namespaces, you just need to know how to use them. See: http://www.sitepoint.com/simplexml-and-namespaces/ and http://stackoverflow.com/q/10322464/206403 – gen_Eric Sep 10 '12 at 14:52

3 Answers3

0

Try with xpath:

$doc     = new DOMDocument;
@$doc->loadHTML($string);
$xpath   = new DOMXpath($doc);
$r       = $xpath->query("//id/@im:id");
$id      = $r->item(0)->value;
Igor Parra
  • 10,214
  • 10
  • 69
  • 101
  • @snow-crash [Error Control Operator](http://php.net/manual/en/language.operators.errorcontrol.php) *"When prepended to an expression in PHP, any error messages that might be generated by that expression will be ignored."* In this case helps to ignore PHP warnings when works over malformed downloaded documents. – Igor Parra Sep 10 '12 at 13:33
0

SimpleXML gives you can easy way to drill down in the XML structure and get the element(s) you want. No need for the regex, whatever it does.

<?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/>';
}

DEMO: http://codepad.viper-7.com/qNo7gs

gen_Eric
  • 223,194
  • 41
  • 299
  • 337
0

Try:

$sxml = new SimpleXMLElement($url);
for($i = 0;$i <=10;$i++){
$appid= $sxml->entry[$i]->id->attributes("im",TRUE);
echo $appid;
}