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

在PHP中对非对象调用成员函数,php,codeigniter,Php,Codeigniter,我正在尝试修改一个我发现是Steam API类的类。我希望它能和codeigniter一起工作。调用getProfileData函数时,问题标题中不断出现错误。不知道为什么会这样。代码如下: 图书馆: <?php // Disable XML warnings to avoid problems when SteamCommunity is down libxml_use_internal_errors(true); // Use SteamUtility to fetch URLs an

我正在尝试修改一个我发现是Steam API类的类。我希望它能和codeigniter一起工作。调用getProfileData函数时,问题标题中不断出现错误。不知道为什么会这样。代码如下:

图书馆:

<?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!\n\nException Info:\n" . $e . "\n\n";
            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;

    }
转储$member变量将返回以下结果:

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)
试试这些方法

  • print_r($member['streamid'])检查数据是否为空
  • 如果将数据作为目标数组传递,则需要使用指针
    [0]

    例如:
    $this->streamuser->getProfileData($member[0]['streamid'])
    • 使用
      print\r
    • print_r($member[0]['streamid'])
  • 所以最终的代码是

    print_r($member['steamId']);//check data exist
    print_r($member[0]['steamId']);//check data exist
    
    if($member['steamId'])
    {
    
        $this->load->library('SteamUser');
        $this->SteamUser->getProfileData($member['steamId']);
        $steam = array('id' => $member['steamId'], 'avatar' => $this->SteamUser->avatarIcon);
    
        $info = array_merge($info, $steam);
    
    }
    

    显然,当您使用CodeIgniter加载一个类时,所给对象的名称都是小写的。所以我加载了$this->streamuser->getProfileData(),而我本应该这样做的,比如$this->streamuser->getProfileData()。

    在lib中,getProfileData($userId)参数是变量而不是对象,但当您以$this->streamuser->getProfileData($member['streamid')的形式调用模型时;参数是object type您是否实例化了该类?请尝试
    var_dump($this->streamuser->getProfileData($member['streamid'])使用搜索框,你会找到你的答案。我已经修改了原来的问题。我已经改变了调用和初始化类的方法,还包括$member变量的值。
    
    print_r($member['steamId']);//check data exist
    print_r($member[0]['steamId']);//check data exist
    
    if($member['steamId'])
    {
    
        $this->load->library('SteamUser');
        $this->SteamUser->getProfileData($member['steamId']);
        $steam = array('id' => $member['steamId'], 'avatar' => $this->SteamUser->avatarIcon);
    
        $info = array_merge($info, $steam);
    
    }