douyi0902 2015-09-23 01:28
浏览 372
已采纳

在PHP中调用非对象的成员函数

I am trying to modify a class that I found that is a Steam API class. I want it to work with codeigniter. I keep getting the error in the question title when I call the getProfileData function. Not sure why it's happening. Here is the code:

The library:

<?php
// Disable XML warnings to avoid problems when SteamCommunity is down
libxml_use_internal_errors(true);
// Use SteamUtility to fetch URLs and other stuff
require_once 'SteamUtility.php';

/**
* SteamUser - Representation of any Steam user profile
*
* @category   SteamAPI
* @copyright  Copyright (c) 2012 Matt Ryder (www.mattryder.co.uk)
* @license    GPLv2 License
* @version    v1.3
* @link       https://github.com/MattRyder/SteamAPI/blob/master/steam/SteamUser.php
* @since      Class available since v1.0
*/
class SteamUser {

    private $userID;
    private $vanityURL;
    private $apiKey;
    public $info;

    /**
     * Constructor
     * @param mixed  $id      User's steamID or vanityURL
     * @param string $apiKey  API key for http://steamcommunity.com/dev/
     */
    /**
     * GetProfileData
     * - Accesses Steam Profile XML and parses the data
     */

    function __construct($params){

        $userId = $params['userId'];

        $this->CI =& get_instance();

        $this->CI->load->config('steam');

        if(empty($userId)) {
            echo "Error: No Steam ID or URL given!", PHP_EOL;
            return NULL;
        }
        if(is_numeric($userId)) {
            $this->userID = $userId;
        }
        else {
            $this->vanityURL = strtolower($userId);
        }

        $this->apiKey = $this->CI->config->item('api_key');

    }

    function getProfileData() {

        $info = array();

        //Set Base URL for the query:
        if(empty($this->vanityURL)) {
            $base = "http://steamcommunity.com/profiles/{$this->userId}/?xml=1";
        } else {
            $base = "http://steamcommunity.com/id/{$this->vanityURL}/?xml=1";
        }

        try {
            $content = SteamUtility::fetchURL($base);
            if ($content) {
                $parsedData = new SimpleXMLElement($content);
            } else {
                return null;
            }
        } catch (Exception $e) {
            //echo "Whoops! Something went wrong!

Exception Info:
" . $e . "

";
            return null;
        }

        if(!empty($parsedData)) {
            $info['steamID64'] = (string)$parsedData->steamID64;
            $info['steamID'] = (string)$parsedData->steamID;
            $info['stateMessage'] = (string)$parsedData->stateMessage;
            $info['visibilityState'] = (int)$parsedData->visibilityState;
            $info['privacyState'] = (string)$parsedData->privacyState;

            $info['avatarIcon'] = (string)$parsedData->avatarIcon;
            $info['avatarMedium'] = (string)$parsedData->avatarMedium;
            $info['avatarFull'] = (string)$parsedData->avatarFull;

            $info['vacBanned'] = (int)$parsedData->vacBanned;
            $info['tradeBanState'] = (string)$parsedData->tradeBanState;
            $info['isLimitedAccount'] = (string)$parsedData->isLimitedAccount;

            $info['onlineState'] = (string)$parsedData->onlineState;
            $info['inGameServerIP'] = (string)$parsedData->inGameServerIP;

            //If their account is public, get that info:
            if($info['privacyState'] == "public") {
                $info['customURL'] = (string)$parsedData->customURL;
                $info['memberSince'] = (string)$parsedData->memberSince;

                $info['steamRating'] = (float)$parsedData->steamRating;
                $info['hoursPlayed2Wk'] = (float)$parsedData->hoursPlayed2Wk;

                $info['headline'] = (string)$parsedData->headline;
                $info['location'] = (string)$parsedData->location;
                $info['realname'] = (string)$parsedData->realname;
                $info['summary'] = (string)$parsedData->summary;
            }

            //If they're in a game, grab that info:
            if($info['onlineState'] == "in-game") {
                $info['inGameInfo']['inGameInfo'] = array();
                $info['inGameInfo']["gameName"] = (string)$parsedData->inGameInfo->gameName;
                $info['inGameInfo']["gameLink"] = (string)$parsedData->inGameInfo->gameLink;
                $info['inGameInfo']["gameIcon"] = (string)$parsedData->inGameInfo->gameIcon;
                $info['inGameInfo']["gameLogo"] = (string)$parsedData->inGameInfo->gameLogo;
                $info['inGameInfo']["gameLogoSmall"] = (string)$parsedData->inGameInfo->gameLogoSmall;
            }

            //Get their most played video games:
            if(!empty($parsedData->mostPlayedGames)) {
                $info['mostPlayedGames'] = array();

                $i = 0;
                foreach ($parsedData->mostPlayedGames->mostPlayedGame as $mostPlayedGame) {
                    $info['mostPlayedGames'][$i] = new stdClass();
                    $info['mostPlayedGames'][$i]['gameName'] = (string)$mostPlayedGame->gameName;
                    $info['mostPlayedGames'][$i]['gameLink'] = (string)$mostPlayedGame->gameLink;
                    $info['mostPlayedGames'][$i]['gameIcon'] = (string)$mostPlayedGame->gameIcon;
                    $info['mostPlayedGames'][$i]['gameLogo'] = (string)$mostPlayedGame->gameLogo;
                    $info['mostPlayedGames'][$i]['gameLogoSmall'] = (string)$mostPlayedGame->gameLogoSmall;
                    $info['mostPlayedGames'][$i]['hoursPlayed'] = (string)$mostPlayedGame->hoursPlayed;
                    $info['mostPlayedGames'][$i]['hoursOnRecord'] = (string)$mostPlayedGame->hoursOnRecord;
                    $info['mostPlayedGames'][$i]['statsName'] = (string)$mostPlayedGame->statsName;
                    $i++;
                }
            }

            //Any weblinks listed in their profile:
            if(!empty($parsedData->weblinks)) {
                $this['weblinks'] = array();

                $i = 0;
                foreach ($parsedData->weblinks->weblink as $weblink) {
                    $info['weblinks'][$i]['title'] = (string)$weblink->title;
                    $info['weblinks'][$i]['link'] = (string)$weblink->link;
                    $i++;
                }
            }

            //And grab any subscribed groups:
            if(!empty($parsedData->groups)) {
                $this->groups = array();

                $i = 0;
                foreach ($parsedData->groups->group as $group) {
                    $info['groups'][$i] = array();
                    $info['groups'][$i]['groupID64'] = (string)$group->groupID64;
                    $info['groups'][$i]['groupName'] = (string)$group->groupName;
                    $info['groups'][$i]['groupURL'] = (string)$group->groupURL;
                    $info['groups'][$i]['headline'] = (string)$group->headline;
                    $info['groups'][$i]['summary'] = (string)$group->summary;

                    $info['groups'][$i]['avatarIcon'] = (string)$group->avatarIcon;
                    $info['groups'][$i]['avatarMedium'] = (string)$group->avatarMedium;
                    $info['groups'][$i]['avatarFull'] = (string)$group->avatarFull;

                    $info['groups'][$i]['memberCount'] = (string)$group->memberCount;
                    $info['groups'][$i]['membersInChat'] = (string)$group->membersInChat;
                    $info['groups'][$i]['membersInGame'] = (string)$group->membersInGame;
                    $info['groups'][$i]['membersOnline'] = (string)$group->membersOnline;

                    $i++;
                }

            }
        }

        return $info;

    }

My model where I call it:

function retrieve($member_id = 0){

    $info = array();

    $this->db->select('memberId AS id, facebookId, steamId, userName, emailAddress, dateJoined, dateBorn')
                ->from('members')
                ->where('memberId', $member_id)
                ->limit(1);

    if($query = $this->db->get()){

        if($query->num_rows() > 0){

            $member = $query->row_array();
            var_dump($member);
            $info = $member;

            if($member['steamId'] != ''){

                $this->load->library('SteamUser', array('userId' => $member['steamId']));
                $steam = $this->SteamUser->getProfileData();

                $info['steam'] = array(
                    'id' => $member['steamId'],
                    'avatar' => $steam['avatarIcon']
                );

            }


        }

    }

    $this->info = $info;

}

Dumping the $member variable returns this:

array (size=7)
  'id' => string '11' (length=2)
  'facebookId' => string '' (length=0)
  'steamId' => string 'STEAM_0:1:000000000' (length=17)
  'userName' => string 'John Smith' (length=18)
  'emailAddress' => string '' (length=0)
  'dateJoined' => string '2015-09-23 19:38:17' (length=19)
  'dateBorn' => string '0000-00-00 00:00:00' (length=19)
  • 写回答

2条回答 默认 最新

  • douyun6781 2015-09-28 07:58
    关注

    Apparently, when you load a class with CodeIgniter, the name of the object that is given is all lower case. So I was loading $this->SteamUser->getProfileData() when I should have been doing it like $this->steamuser->getProfileData().

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?