Jump to content

Starting a Steam API website...


soslidesigns

Recommended Posts

But I need help in an issue I have been having for awhile now. Some reason when I try to call for online status it does not work sometimes i can get it to show the default code 1 which means online. But I am trying to get it to say Online and Offline and so on. I do not get what I am doing wrong my website using mysql so not sure if its having trouble reading the database or what.  here is the code...

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

    }
?>

This is basically my entire code that calls the steam api. Here is the echo function I use to try and call it on a profile.php page...

	echo "<br /><strong>Online:</strong> " . $steam->user['onlineoffline'] . " ";

So I would really appreciate it if someone can help me. I been trying to figure out this for the past week and its starting to drive me nuts and loose motivation on my project. Everything else works great minus this one issue. I have pretty much asked everywhere I can think of and no help. I use a function to call my database hint the global $DB; in my functions just to give you anymore information I can. $steam variable I believe is coming from my class at the very top of my code that is were $steam comes from.

 

Thanks

Edited by soslidesigns
Link to comment
Share on other sites

Yeah sorry about not being clear on the post. I think this part of the code is not working when I try to call it on my profile.php page.

function getOnlineStatus() {

        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;

    }

The online part works but when I switch my status to away in the software it does not show up on my website as away stills says online. This is the code I am using to call the above code.

  echo "<br /><strong>Online:</strong> " . $steam->user['onlineoffline'] . " ";

These are the values I am using in my array system....

        // 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()
        );   

Thanks

Link to comment
Share on other sites

The value of  $steam->user['onlineoffline'] is defined by this line

            'onlineoffline' => $content['response']['players'][0]['personastate'] == 1 ? 'Online' : 'Offline',

Here you are harding coding its value to   Online   when   $content['response']['players'][0]['personastate']  is equal to 1 or   Offline  for any other value. So this line line 

 echo "<br /><strong>Online:</strong> " . $steam->user['onlineoffline'] . " "; 

will only output Online or Offline, not the actual status message returned by  your getOnlineStatus() method. That value is stored in  $steam->user['status'] 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.