8

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

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

1
  • Which browser are you using? I ask because I'm just wondering about the (side-issue) with the registered trademark symbol. Commented Jan 1, 2013 at 11:18

2 Answers 2

10

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>";
}
?>
Sign up to request clarification or add additional context in comments.

8 Comments

I attempted $volume = $sxml->buy->volume; echo $volume however nothing was returned, any idea why?
@mhopkins321 First make sure that the $typeID is defined, and second you should call: $sxml->marketstat->type->buy->volume (answer fixed)
somewhat new to php/xml (obviously) if my url was http://api.eve-central.com/api/marketstat?typeid=1230&typeid=1231&regionlimit=10000002 where there would be two different type id's, where in the line of -> would the id go in?
@mhopkins321 you can't pass two $typeID's - one of them will run over the other!
hmmm... that's not a normal HTTP behavior... Let me check.
|
4

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>';

2 Comments

this returns some data that i'm not quite familiar with (though it's probably what truly raw xml looks like?). If wanted to call the node at $xml->sell->average. what would I set my variable to? I attempted $sell = $xml->sell->average however that didn't seem to work
Sorry, changing var_dump to print_r will give you a more friendly result. To access the sell average, you would want to do $xml->marketstat->type->sell->average

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.