Steam WebAPI Display Online Issue...

Ok guys I am new here and am seeking help on my code I am working on for my website. So I hope this is the right place to post if not sorry. Anyways, I am working on a steamcommunity.com related website using their API. I have everything working openid grabbing info and so on. I can not figure out why the status is not working properly for me. Below is most of the code I use for calling steam and inserting into my database and trying to add an online function. I do not get why it does not work. I do not know if I am not calling it right on the profile page or what. So here is the code before I ramble on.
[php]class steam {
public $api = STEAM_API;
public $returnUrl = URL;
public $user = array();

function __construct() {
    $this->setUserInfo();
}

function isLoggedin() {
    if(isset($_SESSION['steam'])) {
        return true;
    }
    return false;
}

function showLogoutButton() {
    echo "<br /><a href=\"logout.php\">Logout</a>"; //logout button
}

function steamlogin()
{
    require_once(INCLUDES.'/openid.php');
    try {
        // Change 'localhost' to your domain name.
        $openid = new LightOpenID($this->returnUrl);
        //dump($openid);
        if(!$openid->mode) {
            if(isset($_GET['login'])) {
                $openid->identity = 'http://steamcommunity.com/openid';
                header('Location: ' . $openid->authUrl());
            }
            echo "<a href=\"?login\"><img src=\"http://steamcommunity-a.akamaihd.net/public/images/signinthroughsteam/sits_small.png\"></a>";
        } elseif($openid->mode == 'cancel') {
            echo 'User has canceled authentication!';
        } else {
            if($openid->validate()) {
                    $id = $openid->identity;
                    $ptn = "/^http:\/\/steamcommunity\.com\/openid\/id\/(7[0-9]{15,25}+)$/";
                    preg_match($ptn, $id, $matches);
                    $_SESSION['loginid'] = $matches[1];
                    //die('Here');
                    header('Location: index.php');
                    exit;
            } else {
                    echo "User is not logged in.\n";
            }
            //die('here we are');
        }
    } catch(ErrorException $e) {
        echo $e->getMessage();
    }
}

function doLogout() {
    if(isset($_POST['steamid'])) {
        unset($_POST['steamid']);
    }
    header('Location: index.php');
    exit;
}	  
	
function setUserInfo() {
    global $DB;
    
    if($this->isLoggedin()) {
    
        // Grab the info from the session
        $userData = $DB->getUserById($_SESSION['steam']);
        if(isset($userData['steamid'])) {
            $this->user = $userData;
        } else {
            unset($_SESSION['steam']);
            die('Could not get user');
        }
    
    } elseif(isset($_SESSION['loginid'])) {
        
    $url = 'http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key='.$this->api.'&steamids='.$_SESSION['loginid'];
    $ch = curl_init();
    curl_setopt( $ch, CURLOPT_URL, $url);
    curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true);
    $content = curl_exec($ch);
    curl_close($ch);
    $content = json_decode($content, true);

    $_SESSION['steam'] = $_SESSION['loginid'];

    // Set the database values
    $values = array(
        'steamid' => $content['response']['players'][0]['steamid'],
        'communityvisibilitystate' => $content['response']['players'][0]['communityvisibilitystate'],
        'profilestate' => $content['response']['players'][0]['profilestate'],
        'personaname' => $content['response']['players'][0]['personaname'],
        'lastlogoff' => isset($content['response']['players'][0]['lastlogoff']) ? $content['response']['players'][0]['lastlogoff'] : '',
        'profileurl' => $content['response']['players'][0]['profileurl'],
        'avatar' => $content['response']['players'][0]['avatar'],
        'avatarmedium' => $content['response']['players'][0]['avatarmedium'],
        'avatarfull' => $content['response']['players'][0]['avatarfull'],
        'personastate' => $content['response']['players'][0]['personastate'],
        'timecreated' => $content['response']['players'][0]['timecreated'],       
        'onlineoffline' => $this->user['personastate'] == 1 ? 'Online' : 'Offline',
        'status' => $this->getOnlineStatus()
    );
    if($values['lastlogoff'] == '') {
        $values['lastlogoff'] = 'NA';
    } else {
        $values['lastlogoff'] = date("m/d/Y", $values['lastlogoff']);
    }      

    // Are updating the user info or adding them?
    $userExists = $DB->getUserById($_SESSION['steam']);
    if(isset($userExists['steamid'])) {
        // We have this user, lets update their info
        $DB->update('users', $values, array('steamid' => $_SESSION['steam']));
    } else {
        // New user, lets insert them
        $id = $DB->insert('users', $values);
    }


    unset($_SESSION['loginid']);
    $this->setUserInfo();
}

}
function getOnlineStatus($values) {

    switch($this->user['onlineoffline']) {

        case '0':

        default:

            $state = 'Offline';

        break;

        case '1':

            $state = 'Online';

        break;

        case '2':

            $state = 'Busy';

        break;

        case '3':

            $state = 'Away';

        break;

        case '4':

            $state = 'Snooze';

        break;

        case '5':

            $state = 'Looking to trade';

        break;

        case '6':

            $state = 'Looking to play';

        break;

    }

    return $state;

}[/php]

This is basically the entire top part of my code that runs the steam data and inserts into my database. Here is what I use to call the online code on my profile page.

[php]echo "
Status? " . $steam->user[‘onlineoffline’] . " "; [/php]

So what did I do wrong and if anyone can help please do so I will try anything lol. I will announce my site once the code is fixed and so on :). This is not another steam/trading website this will be used for any gamer and steam fan!

I have no clue, but I’m going to take a stab in the dark… I’m not a PHP Coder, but is seems to me, doing this would make more sense.

Change:

[php]‘onlineoffline’ => $this->user[‘personastate’] == 1 ? ‘Online’ : ‘Offline’,[/php]

To:

[php]‘onlineoffline’ => $content[‘response’][‘players’][0][‘personastate’] == 1 ? ‘Online’ : ‘Offline’,[/php]

Well that seems to work but when I switch to offline it does not wan’t to switch to offline on the website. It just simply turns on the online text and offline text by switching 1 to 0 or 0 to 1 and so on. I am using this code to try and switch the other online and offline features…

[php]function getOnlineStatus() {

    switch($steam->user['onlineoffline']) {

        case '0':

        default:

            $state = 'Offline';

        break;

        case '1':

            $state = 'Online';

        break;

        case '2':

            $state = 'Busy';

        break;

        case '3':

            $state = 'Away';

        break;

        case '4':

            $state = 'Snooze';

        break;

        case '5':

            $state = 'Looking to trade';

        break;

        case '6':

            $state = 'Looking to play';

        break;

    }

    return $state;

}[/php]

The code again I am using on the profile.php page is this to call the switch function am I using it right or is it setup right? But you are right that code does seem to make it say Online :slight_smile: I just would like to use my Switch statement also any ideas? This is the code I am using on my profile.php page to call the online stuff…
[php]echo "
Status? " . $steam->user[‘onlineoffline’] . " "; [/php]

Thanks

To reflect your other statuses.

You would need to change…

[php] echo "
Status? " . $steam->user[‘onlineoffline’] . " "; [/php]

to

[php] echo "
Status? " . $steam->user[‘status’] . " "; [/php]

I also see you changed

[php] function getOnlineStatus($values) {[/php]

to:

[php] function getOnlineStatus() {[/php]

But What you should really do is change it back and make it say this…

[php] function getOnlineStatus($values) {

     switch($values) {

         case '0':

         default:

             $state = 'Offline';

         break;

         case '1':

             $state = 'Online';

         break;

         case '2':

             $state = 'Busy';

         break;

         case '3':

             $state = 'Away';

         break;

         case '4':

             $state = 'Snooze';

         break;

         case '5':

             $state = 'Looking to trade';

         break;

         case '6':

             $state = 'Looking to play';

         break;

     }

     return $state;

 }[/php]

Then you should change…

[php] ‘status’ => $this->getOnlineStatus()[/php]

to:

[php] ‘status’ => $this->getOnlineStatus($content[‘response’][‘players’][0][‘personastate’])[/php]

Again, I’m taking your word on what the API returns, I didn’t look any of it up on the steam site…

Ok and thank you for the help hopefully your fixes work I will try them now. This stuff has been bugging me all week lol.

Ok it seems to be working but it is not updating this is what my code looks like now…

include/steam.php
[php]function setUserInfo() {
global $DB;

    if($this->isLoggedin()) {
    
        // Grab the info from the session
        $userData = $DB->getUserById($_SESSION['steam']);
        if(isset($userData['steamid'])) {
            $this->user = $userData;
        } else {
            unset($_SESSION['steam']);
            die('Could not get user');
        }
    
    } elseif(isset($_SESSION['loginid'])) {
        
    $url = 'http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key='.$this->api.'&steamids='.$_SESSION['loginid'];
    $ch = curl_init();
    curl_setopt( $ch, CURLOPT_URL, $url);
    curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true);
    $content = curl_exec($ch);
    curl_close($ch);
    $content = json_decode($content, true);

    $_SESSION['steam'] = $_SESSION['loginid'];

    // Set the database values
    $values = array(
        'steamid' => $content['response']['players'][0]['steamid'],
        'communityvisibilitystate' => $content['response']['players'][0]['communityvisibilitystate'],
        'profilestate' => $content['response']['players'][0]['profilestate'],
        'personaname' => $content['response']['players'][0]['personaname'],
        'lastlogoff' => isset($content['response']['players'][0]['lastlogoff']) ? $content['response']['players'][0]['lastlogoff'] : '',
        'profileurl' => $content['response']['players'][0]['profileurl'],
        'avatar' => $content['response']['players'][0]['avatar'],
        'avatarmedium' => $content['response']['players'][0]['avatarmedium'],
        'avatarfull' => $content['response']['players'][0]['avatarfull'],
        'personastate' => $content['response']['players'][0]['personastate'],
        'timecreated' => $content['response']['players'][0]['timecreated'],         
        'onlineoffline' => $content['response']['players'][0]['personastate'] == 1 ? 'Online' : 'Offline',
        'status' => $this->getOnlineStatus($content['response']['players'][0]['personastate'])
    );   
    if($values['lastlogoff'] == '') {
        $values['lastlogoff'] = 'NA';
    } else {
        $values['lastlogoff'] = date("m/d/Y", $values['lastlogoff']);
    }                

    // Are updating the user info or adding them?
    $userExists = $DB->getUserById($_SESSION['steam']);
    if(isset($userExists['steamid'])) {
        // We have this user, lets update their info
        $DB->update('users', $values, array('steamid' => $_SESSION['steam']));
    } else {
        // New user, lets insert them
        $id = $DB->insert('users', $values);
    }


    unset($_SESSION['loginid']);
    $this->setUserInfo();
}

}
function getOnlineStatus($values) {

    switch($values) {

        case '0':

        default:

            $state = 'Offline';

        break;

        case '1':

            $state = 'Online';

        break;

        case '2':

            $state = 'Busy';

        break;

        case '3':

            $state = 'Away';

        break;

        case '4':

            $state = 'Snooze';

        break;

        case '5':

            $state = 'Looking to trade';

        break;

        case '6':

            $state = 'Looking to play';

        break;

    }

    return $state;

}[/php]

profile.php
[php]echo "
Online: " . $steam->user[‘status’] . " ";[/php]

I get no error logs but now it just does not update right away it says Away still even though I switched it back to Online…Should the $state variables be $values as well?

Ok well it looks like it is almost 100% working. I am not sure if its due to my session setup or what. But it will only update the status if i completely log off my website then log back in. Thank you for your help though at least there is no more error logs and it seems to work for now. I gave you karma :).

Sponsor our Newsletter | Privacy Policy | Terms of Service