SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Thread: class problems

  1. #1
    SitePoint Enthusiast
    Join Date
    Sep 2008
    Posts
    71
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    class problems

    i am just getting started messing with classes
    i have this script that works, i just wanted to separate it into parts
    cause i want to expand it and have other things use parts because they all similar things, but get different data and parse differently

    PHP Code:
    class apiinterface
    {
        public 
    $userID;
        public 
    $apiKey;
        
        private 
    $apiroot "example.com";
        
        public function 
    __construct($userID=0,$apiKey=0) {
                
    $this->userID $userID;
                
    $this->apiKey $apiKey;
        }
        public function 
    apiRequest($target$params = array()) {
            
    $paramarray $params;
            
            
    //curl setup
            
            
    $ch curl_init();
            
    curl_setopt($ch,CURLOPT_URL,$this->apiroot.$target);
            
    curl_setopt($ch,CURLOPT_RETURNTRANSFERtrue);
            
    curl_setopt($ch,CURLOPT_POSTtrue);
            
    curl_setopt($ch,CURLOPT_POSTFIELDS$paramarray);
            
            
    //execute curl and close
            
    $res curl_exec($ch);
            
    curl_close($ch);
            
    $xml simplexml_load_string($res);
            return 
    $xml;
        }
    }] 
    PHP Code:
    include '../white/apiinterface.eve.php';

    class 
    eve_chars extends apiinterface {
            private 
    $chars_name;
            private 
    $chars_id;
            private 
    $chars_corp_name;
            private 
    $chars_corp_id;


        function 
    __construct($userID=0,$apiKey=0) {
                
    $this->userID $userID;
                
    $this->apiKey $apiKey;
        }
        
        function 
    getChars(){
            
    $paramarray = array();
            
    $paramarray['userID'] = $this->userID;
            
    $paramarray['apiKey'] = $this->apiKey;
             
    $data $this->apiRequest("xml data"$paramarray);
            
            
    $xml simplexml_load_string($data);
            
    //echo $xml;
            
    echo "<br />";
                foreach(
    $xml->result->rowset->row as $row)
                {
                    
    $chars_name $row['name'];
                    
    $chars_id $row['characterID'];
                    
    $chars_corp_name $row['corporationName']; 
                    
    $chars_corp_id $row['corporationID'];
        
                    echo 
    $chars_name " - " $chars_corp_name "<br />";
                }
            echo 
    "<br />";
            
        }
        function 
    getsource()
        {
            return 
    $chars_name ' - '$chars_corp_name '<br />';
        }
        function 
    __toString()
        {
            return 
    $this->getsource();
        }

    PHP Code:
    $char = new eve_chars($userID$apikey);
        
    $kar $char->GetChars();
        echo 
    "Some Stuff goes here";
        echo 
    $kar
    i get "Invalid argument supplied for foreach()" and some parse errors
    now the original way i had this set up it works
    i had the apiinterface class and i parsed in the index file and that worked
    what i want is to call the class and have it output the info
    i really didn't change anything except adding a new class

    i figured it out to be a problem returning from apiRequest() function
    could someone help point out the problem

  2. #2
    SitePoint Zealot Kayarc's Avatar
    Join Date
    Sep 2009
    Posts
    127
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What happens when you var_dump($xml)
    Phoenix Arizona Web Design | info *at* kayarc.com | 602.633.2676

  3. #3
    SitePoint Enthusiast
    Join Date
    Sep 2008
    Posts
    71
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    (SimpleXMLElement)#2 (4) { ["@attributes"]=> array(1) { ["version"]=> string(1) "2" } ["currentTime"]=> string(19) "2009-09-30 16:52:14" ["error"]=> string(67) "Invalid username and/or password passed to UserData.LoginWebUser()." ["cachedUntil"]=> string(19) "2009-09-30 16:57:14" }

    hmm.. this would mean it isn't getting the paramarray values.

    i should put in the error handleing for getting error from the site giving the xml

    thanks, i forgot about var_dump()


    would the $userID and $apiKey carry over from the apiinterface class, or should i just delcare them again in chars class?

  4. #4
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Drop the __contructor in the child class, it inherits it from its parent.
    PHP Code:
        function getsource()
        {
            return 
    $this->chars_name ' - '$this->chars_corp_name '<br />';
        } 
    get source needs to access like so: $this->vars

  5. #5
    SitePoint Enthusiast
    Join Date
    Sep 2008
    Posts
    71
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok so it works now, i made the changes
    i did something else and it started working lol

    thanks for the help


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •