Read out stats

Hello dear phphelp team.
As i am in the specific section, you should be able to guess that i am a beginner!

id like to read out some highscores from a game called runescape.

[code=php] <?php
//
$RS = new RuneScapeStats(‘Bulletfight2’);
$RS->getSource();
$RS->parseArray();
$stats = $RS->getResult();

echo <<<HTML





HTML;
foreach($stats as $values){
echo n”;
echo ;
echo ;
echo ;
echo n”;
echo n”;
}
echo
Skill Rank Level XP
.$values[‘skill’]. .$values[‘rank’]. .$values[‘level’]. .$values[‘xp’].
;
//
class RuneScapeStats {

    var $server </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> </span><span class="syntaxstring">'hiscore.runescape.com'</span><span class="syntaxkeyword">;

    var $relURL </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> </span><span class="syntaxstring">'hiscorepersonal.ws'</span><span class="syntaxkeyword">;


var $user
;

    var $statsArray</span><span class="syntaxkeyword">;

    var $disciplines </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> array</span><span class="syntaxkeyword">(

‘Overall’,
‘Attack’,
‘Defence’,
‘Strength’,
‘Hitpoints’,
‘Ranged’,
‘Prayer’,
‘Magic’,
‘Cooking’,
‘Woodcutting’,
‘Fletching’,
‘Fishing’,
‘Firemaking’,
‘Crafting’,
‘Smithing’,
‘Mining’,
‘Herblore’,
‘Agility’,
‘Thieving’,
‘Slayer’,
‘Farming’,
‘Runecraft’,
‘Hunter’,
‘Construction’);

var $debug = false;

var $_httpBody
= “”;
var $_source;

    function RunescapeStats</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">$user</span><span class="syntaxkeyword">){

$this->user = urlencode($user);
}

  </span>[/code]

is what i have for now.
I cant get, why it doesnt work… anyways, mayby someone “pro” is able to teach me, what i did wrong…

[code=php] function getSource()
{
$this->addParam(‘user1’, $this->user);
$this->addParam(‘submit’, ‘Search’);

        </span><span class="syntaxdefault">$fp </span><span class="syntaxkeyword">= </span><span class="syntaxdefault">fsockopen</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">$this</span><span class="syntaxkeyword">-></span><span class="syntaxdefault">server</span><span class="syntaxkeyword">, </span><span class="syntaxdefault">80</span><span class="syntaxkeyword">, </span><span class="syntaxdefault">$errno</span><span class="syntaxkeyword">, </span><span class="syntaxdefault">$errstr</span><span class="syntaxkeyword">, </span><span class="syntaxdefault">10</span><span class="syntaxkeyword">);
        if(!</span><span class="syntaxdefault">$fp</span><span class="syntaxkeyword">){
            return </span><span class="syntaxdefault">false</span><span class="syntaxkeyword">;
        }
       
        </span><span class="syntaxdefault">$contentLen </span><span class="syntaxkeyword">= </span><span class="syntaxdefault">strlen</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">$this</span><span class="syntaxkeyword">-></span><span class="syntaxdefault">_httpBody</span><span class="syntaxkeyword">);
        </span><span class="syntaxdefault">$req </span><span class="syntaxkeyword">= <<<REQUEST

POST /{$this->relURL} HTTP/1.1
Host:
{$this->server}
Referer:
{$this->server}/{$this->relURL}
Connection: Keep-Alive
Content-Type: application/x-www-form-urlencoded
Content-Length:
{$contentLen}

</span><span class="syntaxkeyword">{</span><span class="syntaxdefault">$this</span><span class="syntaxkeyword">-></span><span class="syntaxdefault">_httpBody</span><span class="syntaxkeyword">}</span><span class="syntaxdefault">
REQUEST;
        fwrite($fp, $req);
        $stop = false;
        while (!$stop && !feof($fp)) {
            $fetch = fgets($fp, 128);
            $this</span><span class="syntaxkeyword">-></span><span class="syntaxdefault">_source.= $fetch;
            if(strstr($fetch, 'This webpage') !== false){
                fwrite($fp, "Connection: closernrn");
                fclose($fp);
                $stop = true;
            }       
        }
        return true;
    }
   

    function parseArray()
    {
        if (strstr($this</span><span class="syntaxkeyword">-></span><span class="syntaxdefault">_source, "does not feature in the hiscores")) {
            $this</span><span class="syntaxkeyword">-></span><span class="syntaxdefault">statsArray = false;
            return;
        }
       
        $i = 1;
        foreach ($this</span><span class="syntaxkeyword">-></span><span class="syntaxdefault">disciplines as $type) {
                 //echo str_replace("n", "", $this</span><span class="syntaxkeyword">-></span><span class="syntaxdefault">_source);
            if( eregi($type . '</a></td><td align="right">([0-9,]+)</td><td align="right">([0-9]+)</td><td align="right">([0-9,]+)</td>', str_replace("n", "", $this</span><span class="syntaxkeyword">-></span><span class="syntaxdefault">_source), $regs) ){
                $this</span><span class="syntaxkeyword">-></span><span class="syntaxdefault">statsArray[$i]['skill']  = $type;
                  $this</span><span class="syntaxkeyword">-></span><span class="syntaxdefault">statsArray[$i]['rank'] = $regs</span><span class="syntaxkeyword">[</span><span class="syntaxdefault">1</span><span class="syntaxkeyword">]</span><span class="syntaxdefault">;
                  $this</span><span class="syntaxkeyword">-></span><span class="syntaxdefault">statsArray[$i]['level'] = $regs</span><span class="syntaxkeyword">[</span><span class="syntaxdefault">2</span><span class="syntaxkeyword">]</span><span class="syntaxdefault">;
                  $this</span><span class="syntaxkeyword">-></span><span class="syntaxdefault">statsArray[$i]['xp']    = $regs</span><span class="syntaxkeyword">[</span><span class="syntaxdefault">3</span><span class="syntaxkeyword">]</span><span class="syntaxdefault">;
            } else {
                $this</span><span class="syntaxkeyword">-></span><span class="syntaxdefault">statsArray[$i]['skill']  = $type;
                $this</span><span class="syntaxkeyword">-></span><span class="syntaxdefault">statsArray[$i]['rank'] = 0;
                $this</span><span class="syntaxkeyword">-></span><span class="syntaxdefault">statsArray[$i]['level'] = 0;
                $this</span><span class="syntaxkeyword">-></span><span class="syntaxdefault">statsArray[$i]['xp']    = 0;
            }
            $i++;
        }
    }
   

    function addParam($name, $value)
    {
        strlen($this</span><span class="syntaxkeyword">-></span><span class="syntaxdefault">_httpBody)<1 ? $this</span><span class="syntaxkeyword">-></span><span class="syntaxdefault">_httpBody=$name.'='.$value : $this</span><span class="syntaxkeyword">-></span><span class="syntaxdefault">_httpBody.='&'.$name.'='.$value ;
    }
   

    function getResult()
    {
        return $this</span><span class="syntaxkeyword">-></span><span class="syntaxdefault">statsArray;
    }
   
    function execute()
    {
        if ( $this</span><span class="syntaxkeyword">-></span><span class="syntaxdefault">getSource() ) {
            $this</span><span class="syntaxkeyword">-></span><span class="syntaxdefault">parseArray();
        } else {
            $this</span><span class="syntaxkeyword">-></span><span class="syntaxdefault">debug?trigger_error("---"):null;
            $this</span><span class="syntaxkeyword">-></span><span class="syntaxdefault">statsArray = false;
        }
    }   
}




echo '<form action="http://' . $_SERVER</span><span class="syntaxkeyword">[</span><span class="syntaxdefault">'SERVER_NAME'] . $_SERVER</span><span class="syntaxkeyword">[</span><span class="syntaxdefault">'SCRIPT_NAME'] . '" method="get">';
echo 'name:&nbsp;&nbsp;&nbsp;<input type="text" name="name" value="'[email protected]$_GET</span><span class="syntaxkeyword">[</span><span class="syntaxdefault">'name'].'" />';
echo '<br /><input type="submit" value="Highscores anzeigen!" />';
echo '</form><br />';


if(isset($_GET</span><span class="syntaxkeyword">[</span><span class="syntaxdefault">'name'])){
    $RS = new RuneScapeStats($_GET</span><span class="syntaxkeyword">[</span><span class="syntaxdefault">'name']);
    $RS</span><span class="syntaxkeyword">-></span><span class="syntaxdefault">execute();
    $stats = $RS</span><span class="syntaxkeyword">-></span><span class="syntaxdefault">getResult();

    if ($stats != false)
    {
        echo <<<HTML
<table>
    <tr>
        <td><b>Skill</b></td><td><b>Rank</b></td><td><b>Level</b></td><td><b>XP</b></td>
    </tr>
HTML;
        foreach($stats as $values){
            echo "<tr>n";
            echo '<td>'.$values</span><span class="syntaxkeyword">[</span><span class="syntaxdefault">'skill'].'</td>';
            echo '<td>'.$values</span><span class="syntaxkeyword">[</span><span class="syntaxdefault">'rank'].'</td>';
            echo '<td>'.$values</span><span class="syntaxkeyword">[</span><span class="syntaxdefault">'level'].'</td>';
            echo '<td>'.$values</span><span class="syntaxkeyword">[</span><span class="syntaxdefault">'xp']."</td>n";
            echo "</tr>n";
        }
        echo '</table>';
    }
    else
    {
        echo "Name nicht gefunden!";
    }
   
   
}
?></span>[/code]

As i wished to post the whole code in one codebox, i earned a nice php error… maybe you should try to fix that :stuck_out_tongue:

Sponsor our Newsletter | Privacy Policy | Terms of Service