Getting xml from url to variable

I am trying to get an XML file with a url

http://api.eve-central.com/api/marketstat?typeid=1230&regionlimit=10000002 

but seems to be failing. I tried

However, none of them seem to satisfy a good XML feed if I am either echo or print_r . The ultimate goal is to ultimately parse this data, but getting into a variable is likely to be a good start.

I have attached my code below. This is contained in the loop, and $typeID really gives the correct identifier, as shown above.

 $url = 'http://api.eve-central.com/api/marketstat?typeid='.$typeID.'&regionlimit=10000002'; echo $url."<br />"; $xml = new SimpleXMLElement($url); print_r($xml); 

I have to say that the other strange thing I see is that when I echo $ url, I get

 http://api.eve-central.com/api/marketstat?typeid=1230®ionlimit=10000002 

® is a registered trademark. I'm not sure if this is a “function” in my browser or a “function” in my code

+8
xml php parsing simplexml
source share
2 answers

Try the following:

 <?php $typeID = 1230; // set feed URL $url = 'http://api.eve-central.com/api/marketstat?typeid='.$typeID.'&regionlimit=10000002'; echo $url."<br />"; // read feed into SimpleXML object $sxml = simplexml_load_file($url); // then you can do var_dump($sxml); // And now you'll be able to call `$sxml->marketstat->type->buy->volume` as well as other properties. echo $sxml->marketstat->type->buy->volume; // And if you want to fetch multiple IDs: foreach($sxml->marketstat->type as $type){ echo $type->buy->volume . "<br>"; } ?> 
+9
source share

You need to get the data from the URL in order to create an XML object.

 $url = 'http://api.eve-central.com/api/marketstat?typeid='.$typeID.'&regionlimit=10000002'; $xml = new SimpleXMLElement(file_get_contents($url)); // pre tags to format nicely echo '<pre>'; print_r($xml); echo '</pre>'; 
+4
source share

All Articles