runestation Posted January 28, 2010 Share Posted January 28, 2010 I'm fairly new to PHP so I apologize in advance for my noobiness. I'm working on a calculator for a game called RuneScape and I want it to get information from http://hiscore.runescape.com/index_lite.ws?player=zorlok7 (making the last part, the name, editable) That page is the highscores in a simple version of just the numbers. What I want to do is be able to get one certain number, and plug it into the calculator. The part I need help with is being able to get one number from the url. I think I'd use file() to put them into an array and then somehow only show one of the numbers. Here is the code: <head> </head> <body><center> <form method="post"> <table width="800" border="1" align="center"> <tr> <td align="center"><label>UserName: <input type="text" name="username"> <input type="submit" value="HS Look up"> <br> </label></td> </tr> <tr> <td height="100" align="center"><label> Current XP: </label> <label> <textarea cols="50" rows="7"> <?php $rs = file('http://hiscore.runescape.com/index_lite.ws?player=zorlok7'); echo "$rs"; ?> </textarea> </label> </td></tr></table></form> </center> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/190064-gamer-calculator/ Share on other sites More sharing options...
oni-kun Posted January 28, 2010 Share Posted January 28, 2010 $name = "zorlok7"; $filecontents = file_get_contents("http://hiscore.runescape.com/index_lite.ws?player=$name"); $stats = explode(',' , $filecontents); echo '<pre>'; echo print_r($stats); But anyway, You'd rather use a preg_match pattern to grab the score with the obvious greater accuracy and be able to parse much more. EDIT: Updated code to do something useful, if you'd not want to use preg_match. Try running the above code to see the output, as you can see it makes a nice array of every stat there. Quote Link to comment https://forums.phpfreaks.com/topic/190064-gamer-calculator/#findComment-1002782 Share on other sites More sharing options...
dmikester1 Posted January 28, 2010 Share Posted January 28, 2010 Here is some code I whipped up for you. You can use this with oni-kun's code. Mike <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8"/> <title>Hi-score lookup</title> </head> <body> <html> <?php if(isset($_POST['username'])) { $user = $_POST['username']; $homepage = file_get_contents('http://hiscore.runescape.com/index_lite.ws?player=' . $user); echo $homepage; } else { ?> <form id="form" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"> Enter your username: <input type="text" id="username" name="username" /> <input type="submit" value="Submit" /> </form> <?php } ?> </html> </body> Quote Link to comment https://forums.phpfreaks.com/topic/190064-gamer-calculator/#findComment-1002805 Share on other sites More sharing options...
runestation Posted January 28, 2010 Author Share Posted January 28, 2010 Hey, thanks for the reply, to the both of you that did. It really got me over that barrier I was stuck on for ... hours. Anyways, I had a further question about the code the first guy posted: $name = "zorlok7"; $filecontents = file_get_contents("http://hiscore.runescape.com/index_lite.ws?player=$name"); $stats = explode(',' , $filecontents); echo '<pre>'; echo print_r($stats); Ok so my question about this is, when it makes a new array at each comma, the only problem about that is, if you view the source of: http://hiscore.runescape.com/index_lite.ws?player=zorlok7 You'll notice there isn't a comma after the 3rd one of each line because it breaks. When I'm trying to get the experience (which is the third one of each line) to show up, it shows it, then breaks and will show the rank (first one of each line) on the line under it. Is there anyway to fix this so it only shows the experience? Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/190064-gamer-calculator/#findComment-1002812 Share on other sites More sharing options...
oni-kun Posted January 28, 2010 Share Posted January 28, 2010 $name = "zorlok7"; $filecontents = file_get_contents("http://hiscore.runescape.com/index_lite.ws?player=$name"); $stats = explode(',', $filecontents); $exp = explode("\n", $stats[2]); echo $exp[0]; //209351778 Note the exp is separated by a newline, not a space (as I did in that example). You'd just need to find the index of the other lines, you can't explode them as newlines as there are internewlines, so finding $stats[30] for example is what you'll have to do for each line. Quote Link to comment https://forums.phpfreaks.com/topic/190064-gamer-calculator/#findComment-1002816 Share on other sites More sharing options...
ignace Posted January 28, 2010 Share Posted January 28, 2010 Go to http://services.runescape.com/m=hiscore/hiscorepersonal.ws and enter your name type everything you see under Skills into the $skills array I already provided a few examples: if (isset($_POST['username'])) { $flags = FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES; if ($stats = file('http://hiscore.runescape.com/index_lite.ws?player=' . $_POST['username'], $flags)) { $skills = array('overall', 'attack', 'defence', 'strength', 'hitpoints', 'ranged', ..); $player = array(); foreach ($stats as $line) { $key = current($skills); $player[$key] = array_map('trim', explode(',', $line)); //the below line only works if minigames stats are left out //this would allow you to do: $player['overall']['rank'] instead of $player['overall'][0]; //$player[$key] = array_combine(array('rank', 'level', 'xp'), array_map('trim', explode(',', $line))); if (!next($skills)) break;// no skills left } print_r($player);// 'overall' => array ( 0 => 11767, 1 => 2138, 2 => 209351778 ) .. } else { //error: username $_POST['username'] not found or highscores temporarily unavailable } } Have fun! Quote Link to comment https://forums.phpfreaks.com/topic/190064-gamer-calculator/#findComment-1002942 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.