So what is happening in the code below is I am grabbing highscore data from a game at this link:
http://hiscore.runescape.com/index_lite.ws?player=
Everything works fine when using a correct username at the end of the url, such as "n0tch". But when the username doesn't exist, it displays the game's website. What I need is a way to check if the string exists on the page.
Error I am getting:
To help yourself better understand, I would try entering these urls into your browser:
Existing & working username:
http://hiscore.runescape.com/index_lite.ws?player=n0tch
Non-existing & throws an error when called in PHP:
http://hiscore.runescape.com/index_lite.ws?player=asdfse123d
I need to add some kind of error checking statement around the foreach loop. Currently I am trying to get the response code of the site but it is 200 every time.
Code:
<?php
$rsn=$_GET["rsn"];
$stats=array("Overall","Attack","Defence","Strength","Hitpoints","Ranged","Prayer","Magic","Cooking","Woodcutting","Fletching","Fishing","Firemaking","Crafting","Smithing","Mining","Herblore","Agility","Thieving","Slayer","Farming","Runecrafting","Hunter","Construction","Summoning","Dungeoneering","Duel Tournament","Bounty Hunters","Bounty Hunters Rougue","Fist Of Guthix","Mobilising Armies");
$url="http://hiscore.runescape.com/index_lite.ws?player=".$rsn;
$a="0";
echo "<pre>Showing Stats for: ".$rsn."\r\n";
echo "<center>
<table>
<tr>
<th>Skill</th>
<th>Rank</th>
<th>Level</th>
<th>Experience</th>
</tr>";
// var_dump($http_response_header);
var_dump(http_response_code());
if (http_response_code(200)) {
$data=explode(chr(10),file_get_contents($url));
foreach ($data as $value) {
if ($a<26) {
$value=str_replace(",","</td><td>",$value);
$pic = "<img class='img-resize' src='./img/skills/".$stats[$a].".png'>";
echo "<tr><td id='firstRow'>".$pic." ".$stats[$a].":</td> ".str_replace("-1 -1","Not Ranked",str_replace("-1 -1 -1","Not Ranked","<td>".$value."</td></tr>"))."\r\n";
}
$a++;
}
echo "</table></center>";
echo "Done";
} else {
echo "didn't work";
}
?>