Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getting xml from URL into variable

I am trying to get an xml feed from a url

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

but seem to be failing miserably. I have tried

  • new SimpleXMLElement,

  • file_get_contents and

  • http_get

Yet none of these seem to echo a nice XML feed when I either echo or print_r. The end goal is to eventually parse this data but getting it into a variable would sure be a nice start.

I have attached my code below. This is contained within a loop and $typeID does in fact give the correct ID as seen above

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

I should state that the other strange thing I am seeing is that when I echo $url, i get

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

the &reg is the registered trademark symbol. I am unsure if this is "feature" in my browser, or a "feature" in my code

like image 423
mhopkins321 Avatar asked Dec 31 '12 20:12

mhopkins321


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>";
}
?>
like image 55
Nir Alfasi Avatar answered Oct 20 '22 00:10

Nir Alfasi


You need to fetch the data from the URL in order to make 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>';
like image 27
Supericy Avatar answered Oct 19 '22 22:10

Supericy