-1

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."&nbsp;".$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";
  }
?>
Charlie
  • 90
  • 9
  • possible duplicate of [HTTP requests with file\_get\_contents, getting the response code](http://stackoverflow.com/questions/15620124/http-requests-with-file-get-contents-getting-the-response-code) – Devon Bessemer Apr 26 '15 at 18:28
  • @Devon I don't understand that answer given. The varDump how would I set that up? I'm still new to php just trying to learn – Charlie Apr 26 '15 at 18:31
  • Try the var_dump and see what variables are available. The PHP manual will come in handy. – Devon Bessemer Apr 26 '15 at 18:31
  • 1
    I suggest you check the return value of file_get_contents and act accordingly. If the server returns a 404 message, file_get_contents will return false and trigger a warning, otherwise it will return the contents of the page. – Hassan Apr 26 '15 at 18:37
  • @Devon So I used it and it outputted some stuff on my screen: http://prntscr.com/6ygr2u But as I said before I'm kinda new to this so I really don't know what to do from this point. – Charlie Apr 26 '15 at 18:41
  • @Chawbee, CURL provides a lot more options for HTTP. Eight may be right, that file_get_contents will return false if an error occurs, so you can check against that. I don't use file_get_contents for HTTP resources. The response code is what you should normally look for, 200 is OK for GET, 400 is a bad request, 404 is file not found, etc. Look up HTTP response codes. – Devon Bessemer Apr 26 '15 at 18:49
  • @Devon Ok so I dumped the http_response_code both when the username exists and when it doesn't exist. Both times it was response code 200. I also added an if statement around my foreach loop to check the response code; but since it is 200 every time it is redundant. I edited my code in the original post to show what I did. – Charlie Apr 26 '15 at 19:12

2 Answers2

1

It is because different structure is given. When payer exists you get some blank page with numbers, when not exists, server returns 404 reponse with redirect. So you have to ensure what content is got from server before exploding. Add some validations and try catch block. Either use curl.

jskidie
  • 154
  • 6
1

Here is simple function that checks against response headers by get_headers()

function getHeadersCheck($url)
{
    $getHeaders=@get_headers($url);

    if(is_array($getHeaders))
    {
        $intersect = array('HTTP/1.0 200 OK','HTTP/1.1 200 OK');
        $result = array_intersect($intersect, $getHeaders);
    }

    if(isset($result) && is_array($result))
        return((empty($result) || count($result)===0) ? false:true);
    else return false;
}

And here is how Your code migh look like with this function...

$rsn=$_GET["rsn"]; 
$url="http://hiscore.runescape.com/index_lite.ws?player=".$rsn; 

if(getHeadersCheck($url))
{
 $data=explode(chr(10),file_get_contents($url));
} 
else $data = null;

if($data === null) echo 'Sorry, can\'t display results';
else
if (is_array($data))
{
 $stats = '(Whatever stats should represent.. )';
 foreach($data as $value)
 {

  // Do whatever You want to do.. 

 }

}
Spooky
  • 1,235
  • 15
  • 17