Go Back   Steam Users' Forums > Steam Discussions > Steam Community Discussion

Reply
 
Thread Tools Display Modes
Old 04-23-2012, 11:20 PM   #1
Mezo
 
 
 
Join Date: Feb 2008
Reputation: 25
Posts: 352
Talking [php] Get Steam status for website help [Soloved]

*** LOOK AT THE BOTTOM FOR THE SOLUTION - POST 12 ***


Can anyonw help me get it working?

I put in my steam ID and now it dont work... pretty sure I used this script before, does anyone know whats wrong?

(trying to add an online message on my website that tells my friends if im offline/online)


Quote:
<?php
$steam_id = "STEAM_0:0:3239553";

if (preg_match('/^STEAM_[0-9]:[0-9]:[0-9]{1,}/i', $steam_id))
{
$steam_id = str_replace("_", ":", $steam_id);
list($part_one, $part_two, $part_three, $part_four) = explode(':', $steam_id);
$result = bcadd('76561197960265728', $part_four * 2);
$result = bcadd($result, $part_two);
$id = bcadd($result, $part_three);
}
else
{
echo "Error. Could not decode SteamID";
}

//$steam_id64 = "$result, $part_three";

// $id = $steam_id64; // google for how you can get the steamID64 out of a Steam_0:0:0XXXX ID
$xml = @simplexml_load_file("http://steamcommunity.com/profiles/".$id."/?xml=1");
switch($xml->onlineState)
{
case "online":
echo "Steam status: <span class=\"steam_on\"><a href='http://steamcommunity.com/profiles/".$id."/' target='_blank'>Online</a></span>";
break;
case "in-game":
echo "Steam status: <span class=\"steam_ing\"><a href='http://steamcommunity.com/profiles/".$id."/' target='_blank'>".$xml->stateMessage."</a></span>";
break;
default:
echo "Steam status: <span class=\"steam_off\"><a href='http://steamcommunity.com/profiles/".$id."/' target='_blank'>Offline</a></span>";
break;
}
?>
I'm not super good with php, so if anyone knows the error, can youpost the fix for it :S Thanks!

Last edited by Mezo: 04-30-2012 at 03:57 PM.
Mezo is offline  
Reply With Quote
Old 04-24-2012, 12:12 AM   #2
slazer2k5
 
 
 
Join Date: Jan 2012
Reputation: 188
Posts: 241
What error do you mean? Returning:

Quote:
Steam status: <span class="steam_on"><a href='http://steamcommunity.com/profiles/76561197966744834/' target='_blank'>Online</a></span>
Edit:

I optimized your script:

Code:
  // Function returns Steam status (default timeout: 5 seconds)
  function get_steam_status($steamID64, $timeout = 5) {
    $context = stream_context_create(array('http' => array('timeout' => $timeout)));
    $file = @file_get_contents('http://steamcommunity.com/profiles/' . $steamID64 . '/?xml=1', false, $context);
    $xml = simplexml_load_string($file);
    if (isset($xml->onlineState)) {
      $online_state = (string)$xml->onlineState;
      $state_message = ($online_state == 'offline' ? 'Offline' : (string)$xml->stateMessage);
    } else {
      // Error loading profile
      $online_state = 'offline';
      $state_message = 'Offline';
    }
    $state_css = array('online' => 'on', 'in-game' => 'ing', 'offline' => 'off');
    return 'Steam status: <span class="steam_' . $state_css[$online_state] . '"><a href="http://steamcommunity.com/profiles/' . $steamID64 . '/" target="_blank">' . $state_message . '</a></span>';
  }

  // ID = Last part of your profile url (http://steamcommunity.com/profiles/76561197966744834)
  echo get_steam_status('76561197966744834');
You don't need to calculate your 64-bit SteamID every time, just enter the one from your profile url. I also added a timeout, you don't want your site visitors having to wait like a minute when the steam servers are overloaded (You could also use cURL to get the profile or regex to read the state, bit I used the same functions as in your original code...)

Last edited by slazer2k5: 04-24-2012 at 12:37 AM.
slazer2k5 is offline   Reply With Quote
Old 04-24-2012, 01:06 PM   #3
Mezo
 
 
 
Join Date: Feb 2008
Reputation: 25
Posts: 352
Talking

Quote:
Originally Posted by slazer2k5 View Post
What error do you mean? Returning:



Edit:

I optimized your script:

Code:
  // Function returns Steam status (default timeout: 5 seconds)
  function get_steam_status($steamID64, $timeout = 5) {
    $context = stream_context_create(array('http' => array('timeout' => $timeout)));
    $file = @file_get_contents('http://steamcommunity.com/profiles/' . $steamID64 . '/?xml=1', false, $context);
    $xml = simplexml_load_string($file);
    if (isset($xml->onlineState)) {
      $online_state = (string)$xml->onlineState;
      $state_message = ($online_state == 'offline' ? 'Offline' : (string)$xml->stateMessage);
    } else {
      // Error loading profile
      $online_state = 'offline';
      $state_message = 'Offline';
    }
    $state_css = array('online' => 'on', 'in-game' => 'ing', 'offline' => 'off');
    return 'Steam status: <span class="steam_' . $state_css[$online_state] . '"><a href="http://steamcommunity.com/profiles/' . $steamID64 . '/" target="_blank">' . $state_message . '</a></span>';
  }

  // ID = Last part of your profile url (http://steamcommunity.com/profiles/76561197966744834)
  echo get_steam_status('76561197966744834');
You don't need to calculate your 64-bit SteamID every time, just enter the one from your profile url. I also added a timeout, you don't want your site visitors having to wait like a minute when the steam servers are overloaded (You could also use cURL to get the profile or regex to read the state, bit I used the same functions as in your original code...)
Thank you! but can u put it in "one whole php code" =O Not sure where i should hmm put in your optimized code you fixed for me
(Yea I'm bad on php hehe)
+ rep!
Mezo is offline   Reply With Quote
Old 04-24-2012, 01:33 PM   #4
slazer2k5
 
 
 
Join Date: Jan 2012
Reputation: 188
Posts: 241
Hm, it's one code, only the start- and ending-tag was missing:
Code:
<?php
  // Function returns Steam status (default timeout: 5 seconds)
  function get_steam_status($steamID64, $timeout = 5) {
    $context = stream_context_create(array('http' => array('timeout' => $timeout)));
    $file = @file_get_contents('http://steamcommunity.com/profiles/' . $steamID64 . '/?xml=1', false, $context);
    $xml = simplexml_load_string($file);
    if (isset($xml->onlineState)) {
      $online_state = (string)$xml->onlineState;
      $state_message = ($online_state == 'offline' ? 'Offline' : (string)$xml->stateMessage);
    } else {
      // Error loading profile
      $online_state = 'offline';
      $state_message = 'Offline';
    }
    $state_css = array('online' => 'on', 'in-game' => 'ing', 'offline' => 'off');
    return 'Steam status: <span class="steam_' . $state_css[$online_state] . '"><a href="http://steamcommunity.com/profiles/' . $steamID64 . '/" target="_blank">' . $state_message . '</a></span>';
  }

  // ID = Last part of your profile url (http://steamcommunity.com/profiles/76561197966744834)
  echo get_steam_status('76561197966744834');
?>
Or what do you mean?
slazer2k5 is offline   Reply With Quote
Old 04-25-2012, 05:11 AM   #5
Mezo
 
 
 
Join Date: Feb 2008
Reputation: 25
Posts: 352
Thanks!
Yea, it was so short and looked so different from the one I posted I thought it was not the complete code hehe.

but

I took the code, put it in a .txt then renamed it to test.php and uploaded it to test it and it shows me as offline when im online...
Any ideas?

http://splitzgames.com/terraria/test.php
Mezo is offline   Reply With Quote
Old 04-25-2012, 05:41 AM   #6
Mangr0v3
 
 
 
Join Date: Sep 2008
Reputation: 4313
Posts: 13,608
I don't know what you changed on your site, but the exact code slazer2k5 posted is working for me.

Code:
[netshroud@sunfire:~] $ php test.php
Steam status: <span class="steam_on"><a href="http://steamcommunity.com/profiles/76561197966744834/" target="_blank">Online</a></span>[netshroud@sunfire:~] $
Mangr0v3 is offline   Reply With Quote
Old 04-25-2012, 06:07 AM   #7
slazer2k5
 
 
 
Join Date: Jan 2012
Reputation: 188
Posts: 241
Quote:
Originally Posted by Mezo View Post
Any ideas?
Upload a file with the following content to your server and write the result down here:
Code:
<?php
  echo ini_get('allow_url_fopen');
?>
slazer2k5 is offline   Reply With Quote
Old 04-25-2012, 06:44 AM   #8
Mezo
 
 
 
Join Date: Feb 2008
Reputation: 25
Posts: 352
Mangr0v3
I didn't change anything in the code, as I said i posted the code just as it was in a txt document i renamed to test.php and uploaded. with no extra in it or anything left out.

slazer2k5
I took the new short code, pasted it into an blank .php file, then uploaded and nothing shows on it..

Last edited by Mezo: 04-26-2012 at 03:34 AM.
Mezo is offline   Reply With Quote
Old 04-25-2012, 08:11 AM   #9
slazer2k5
 
 
 
Join Date: Jan 2012
Reputation: 188
Posts: 241
Quote:
Originally Posted by Mezo View Post
I took the new short code, pasted it into an blank file and uploaded and nothing shows on it..
Hm I expected a 0 (disabled) or 1 (enabled)...

Try this code, it's using another method to download the profile data, perhaps the one used before is deactivated on your server:

Code:
<?php
  // Function returns Steam status (default timeout: 5 seconds)
  function get_steam_status($steamID64, $timeout = 5) {
    $ch = curl_init();
    $curl_options = array(
      CURLOPT_URL => 'http://steamcommunity.com/profiles/' . $steamID64 . '/?xml=1',
      CURLOPT_RETURNTRANSFER => true,
      CURLOPT_FOLLOWLOCATION => true,
      CURLOPT_MAXREDIRS => 3,
      CURLOPT_TIMEOUT => $timeout
    );
    curl_setopt_array($ch, $curl_options);
    $data = curl_exec($ch);
    $xml = @simplexml_load_string($data);
    if (isset($xml->onlineState)) {
      $online_state = (string)$xml->onlineState;
      $state_message = ($online_state == 'offline' ? 'Offline' : (string)$xml->stateMessage);
    } else {
      // Error loading profile
      $online_state = 'offline';
      $state_message = 'Offline';
    }
    $state_css = array('online' => 'on', 'in-game' => 'ing', 'offline' => 'off');
    return 'Steam status: <span class="steam_' . $state_css[$online_state] . '"><a href="http://steamcommunity.com/profiles/' . $steamID64 . '/" target="_blank">' . $state_message . '</a></span>';
  }

  // ID = Last part of your profile url (http://steamcommunity.com/profiles/76561197966744834)
  echo get_steam_status('76561197966744834');
?>
slazer2k5 is offline   Reply With Quote
Old 04-26-2012, 12:09 AM   #10
Mezo
 
 
 
Join Date: Feb 2008
Reputation: 25
Posts: 352
I noticed it says:
"I am: In-Game
Terraria on Steam"

It makes 2 lines when you launched a game... how to make it into one single line of text.

And yes, I changed the line to
Code:
return 'I am: <span class="steam_' . $state_css[$online_state] . '"><a href="http://steamcommunity.com/profiles/' . $steamID64 . '/" target="_blank">' . $state_message . '</a> on Steam</span>';
Do you know what the solution would be to keep the text in one single line?

And once again thanks so much for your effort on helping me
Appreciate it much!

Last edited by Mezo: 04-26-2012 at 03:33 AM.
Mezo is offline   Reply With Quote
Old 04-26-2012, 01:15 AM   #11
s333ftricky
 
 
 
Join Date: Jul 2010
Reputation: 266
Posts: 517
Change (string)$xml->stateMessage to str_replace('<br />', ' ', $xml->stateMessage) that'll remove line break for you

Last edited by s333ftricky: 04-26-2012 at 01:18 AM.
s333ftricky is offline   Reply With Quote
Old 04-26-2012, 03:30 AM   #12
Mezo
 
 
 
Join Date: Feb 2008
Reputation: 25
Posts: 352
Thanks mr s333ftricky and everyone who helped me

The script is now open on http://splitzgames.com/terraria/

And here is the final code on "Display steam status on webpage" for google purpose.

You can try 2 codes!
CODE 1 - (Used on my website).


Code:
<?php
  // Function returns Steam status (default timeout: 5 seconds)
  function get_steam_status($steamID64, $timeout = 5) {
    $ch = curl_init();
    $curl_options = array(
      CURLOPT_URL => 'http://steamcommunity.com/profiles/' . $steamID64 . '/?xml=1',
      CURLOPT_RETURNTRANSFER => true,
      CURLOPT_FOLLOWLOCATION => true,
      CURLOPT_MAXREDIRS => 3,
      CURLOPT_TIMEOUT => $timeout
    );
    curl_setopt_array($ch, $curl_options);
    $data = curl_exec($ch);
    $xml = @simplexml_load_string($data);
    if (isset($xml->onlineState)) {
      $online_state = (string)$xml->onlineState;
      $state_message = ($online_state == 'offline' ? 'Offline' : str_replace('<br />', ' ', $xml->stateMessage));
    } else {
      // Error loading profile
      $online_state = 'offline';
      $state_message = 'Offline';
    }
    $state_css = array('online' => 'on', 'in-game' => 'ing', 'offline' => 'off');
    return 'I am: <span class="steam_' . $state_css[$online_state] . '"><a href="http://steamcommunity.com/profiles/' . $steamID64 . '/" target="_blank">' . $state_message . '</a> on Steam</span>';
  }

  // ID = Last part of your profile url (http://steamcommunity.com/profiles/76561197966744834)
  echo get_steam_status('76561197966744834');
?>

CODE 2
Code:
<?php
  // Function returns Steam status (default timeout: 5 seconds)
  function get_steam_status($steamID64, $timeout = 5) {
    $context = stream_context_create(array('http' => array('timeout' => $timeout)));
    $file = @file_get_contents('http://steamcommunity.com/profiles/' . $steamID64 . '/?xml=1', false, $context);
    $xml = simplexml_load_string($file);
    if (isset($xml->onlineState)) {
      $online_state = (string)$xml->onlineState;
      $state_message = ($online_state == 'offline' ? 'Offline' : (string)$xml->stateMessage);
    } else {
      // Error loading profile
      $online_state = 'offline';
      $state_message = 'Offline';
    }
    $state_css = array('online' => 'on', 'in-game' => 'ing', 'offline' => 'off');
    return 'Steam status: <span class="steam_' . $state_css[$online_state] . '"><a href="http://steamcommunity.com/profiles/' . $steamID64 . '/" target="_blank">' . $state_message . '</a></span>';
  }

  // ID = Last part of your profile url (http://steamcommunity.com/profiles/76561197966744834)
  echo get_steam_status('76561197966744834');
?>
All Thanks to slazer2k5 and s333ftricky for the final change

Last edited by Mezo: 04-30-2012 at 03:59 PM.
Mezo is offline   Reply With Quote
Old 05-01-2012, 03:30 AM   #13
LeCat
 
Join Date: Nov 2011
Reputation: 0
Posts: 14
Thumbs down

response from steam about Steam Community XML

Quote:
We have changed the way Steam Community XML feeds work and there is now effectively a global rate limit at which the XML feeds can be requested. If you or others make feed requests quickly, then yes, some of them will get 503 errors. We had to make this change to reduce load and to allow Steam Community to be responsive even when there are lots of XML feed requests. I’d recommend moving to the Steam web APIs as we plan on deprecating the community XML feeds soon.
LeCat is offline   Reply With Quote
Old 05-01-2012, 05:55 AM   #14
slazer2k5
 
 
 
Join Date: Jan 2012
Reputation: 188
Posts: 241
Perhaps this should be an answer for this thread :P
slazer2k5 is offline   Reply With Quote
Old 05-01-2012, 09:32 AM   #15
Mr SmokesAlot
 
 
 
Banned
Join Date: Apr 2012
Reputation: 209
Posts: 1,167
^ Mine only fetch a new xml every 5 mins tops, the rest of the time it'll use a cache from the server it runs on. So it matters not to me. 60/5x24 = 288, so that leaves me with 99.712 fetches before hitting the limit lol. Also if a 503 error comes up, it'll retry to get a new xml until the xml is no longer 0 bytes on the server.

It's all about programming properly and thinking ahead.

Last edited by Mr SmokesAlot: 05-01-2012 at 10:47 AM.
Mr SmokesAlot is offline   Reply With Quote
Reply

Go Back   Steam Users' Forums > Steam Discussions > Steam Community Discussion


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



All times are GMT -7. The time now is 03:22 PM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.
Site Content Copyright Valve Corporation 1998-2014, All Rights Reserved.