0

I've been using this over 2 months and worked fine until some days ago, when an error message appeared. I use the steam api to get some info of the players.

$url = "http://steamcommunity.com/id/CGaKeepoN/?xml=1";

The page is not blank, it has an xml document. So my first thinking was that my host had turned allow_url_fopen off, but they don't (I asked them).

I also tried using error_reporting(E_ALL); ini_set('display_errors', 1);

And that's what I get:

Warning: simplexml_load_file() [function.simplexml-load-file]: I/O warning : failed to load external entity "" on line 6 Notice: Trying to get property of non-object on line 7

Now I'm using this: $xml = simplexml_load_file(file_get_contents($url));

And I would love to continue using it because installing cURL it's not an option right now. Do you know of a better (or a working) way to get this done? Or how to fix this error?

My full code:

error_reporting(E_ALL);
ini_set('display_errors', 1);
//$url = "http://steamcommunity.com/id/CGaKeepoN/?xml=1";
$url = "xml.txt";
ini_set('allow_url_fopen ','ON');
$xml = file_get_contents($url) or die ("file_get_contents failed");
$xml = simplexml_load_string($xml) or die ("simplexml_load_string failed");
$profilepic = $xml->avatarIcon;
$pic = $xml->avatarFull;
$steamID = $xml->steamID;
$lastonline = $xml->stateMessage;
echo $xml;
echo $profilepic;
echo $pic;
echo $steamID;
echo $lastonline;

EDIT:

If I use the internal url it loads the data, but when I try to use any url that uses http protocol just launches the file_get_contents failed error, even if the url is my website's one. I'm willing to use cURL if there's no other solution. I also thought about making a php script that loads the data and saves it in a file in the server (and then run a cronjob every 10 min), but it would use the file_get_contents anyway...

KeepoN
  • 137
  • 1
  • 11
  • When file_get_contents retrieves the resource, you should use simplexml_load_string for its result. You're not passing a filename there, but literal data. - Of course only if it was successfully fetched. You need to debug the result of file_get_contents. – mario Aug 27 '15 at 23:15
  • I've also tried using simplexml_load_string like I said before, but I keep getting the same error. The problem is in file_get_contents not getting the data. – KeepoN Aug 28 '15 at 08:09
  • 1
    Well, I know. The question is why it doesn't. Many feasible reasons, such as user-agent blocking. Just testing it in the browser doesn't really tell anything. Use `curl -V`/`wget -S` to debug it from the command-line, then `get_headers()` from within PHP and that server to see if you're banned from stream API polling. – mario Aug 28 '15 at 08:12
  • @mario, I'll try it, but it's impossible that I got banned from the api because it's 100% public and I checked out and can access it – KeepoN Aug 28 '15 at 15:49

1 Answers1

0

file_get_content returns a string so use simplexml_load_string instead.

This code works for me, tested.

$url = "http://steamcommunity.com/id/CGaKeepoN/?xml=1";
$xml = simplexml_load_string(file_get_contents($url));
$profilepic = $xml->avatarIcon;
$pic = $xml->avatarFull;
$steamID = $xml->steamID;
$lastonline = $xml->stateMessage;
var_dump($url);
var_dump($xml); //-> string(45) "http://steamcommunity.com/id/CGaKeepoN/?xml=1" bool(false)
echo $xml;
echo $profilepic;
echo $pic;
echo $steamID;
echo $lastonline;
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
  • What is exactly what you get? Because I don't get any errors, but string is blank when it shouldn't... – KeepoN Aug 28 '15 at 21:52