471,092 Members | 1,005 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,092 software developers and data experts.

Obtaining info from a site using PHP

I've searched using several terms, but couldn't find my answer. If this question has been asked before, I appologise.

I'm trying to use PHP to get some info from a website, the website sometimes contains 1 or 2 more pages:
http://nw4.novaworld.net/bhd_3_list.lob?page=0

What I want is, as soon as there is a 'Game Name' containing {a.s.u}, make a PHP script get the 'Game Type' and 'Total Players' next to it, and format the info like this:

Game Name
Game Type
Total Players
Online (Or display Offline if there is no Game Name containing {a.s.u})

I actually don't have a clue where to start with, as my experience with PHP is very low. I was thinking using a Get command, but I don't know if such a command is possible with PHP ?

Any help, tips or info about how I could get this done is very appreciated.
Thanks in advance,

Evolution445
Jul 31 '08 #1
8 1571
Atli
5,058 Expert 4TB
You could get the HTML markup of that page by using the file_get_contents function.

Then it would just be a matter of parsing the HTML into something useful.
I would recommend looking into regular expressions for that, using the PCRE functions.
Jul 31 '08 #2
Ok, I see. If i get this right, my code should start as:

Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3. define('_GAMETYPE', 'Servername:');
  4. define('_PLAYERS', 'Players:');
  5. define('_GAMETYPE', 'Gametype:');
  6. define('_OFFLINE', 'Offline');
  7. define('_ONLINE', 'Online');
  8.  
  9. //********
  10.  
  11. if $file_get_contents( "http://nw4.novaworld.net/bhd_3_list.lob?page=0
  12. " [, FILE_TEXT [, NULL [, OFFSET [, 7 ]]]] ) = "{a.s.u}"
  13.  
maxlen Should thus be 7 because what I'm searching for contains just 7 characters, or should maxlen be NULL cause it has to search a whole page for that ' {a.s.u} ' text ?
And how would I get the Offset where it should read from ?
Sorry if this sounds quite 'nooby' or stupid, but I'm really not experienced with PHP.
Jul 31 '08 #3
Sorry to add this, but on my website I made a small block using HTML of how I want the result to look like:
http://img45.imageshack.us/img45/8514/serverstatus1ez9.png

Now it would only have to be PHP code to update the name, TDM, 0/16 and offline/online. How would I be able to get the exact position of where it has to start obtaining info? As on the page Im attemping to get info from, there's a lot more games using the same gametype (TDM) and maximum players ( 0/16 ):
http://nw4.novaworld.net/bhd_3_list.lob?page=0
Aug 1 '08 #4
Atli
5,058 Expert 4TB
You would have to get the entire HTML of the resource site and have PHP look through the HTML for the information you need.

Like, if I needed the title of Google's main page, I could do:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. // Get the HTML of the page
  3. $html = file_get_contents("http://www.google.com");
  4.  
  5. // Create a regular expressio that looks for the 
  6. //  <title> tags.
  7. $regEx = '/\<title\>(.*)\<\/title\>/is';
  8.  
  9. // Search, using the regular expression
  10. if(preg_match($regEx, $html, $matches)) {
  11.     // Show the title
  12.     echo "The title is: ". $matches[1];
  13. }
  14. else {
  15.     // It failed!
  16.     echo "Didn't work!";
  17. }
  18. ?>
  19.  
You would have to do something similar, replacing the search with something that would look for the info you need.
Aug 1 '08 #5
Thanks alot! This is my code so far:

Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3.  
  4. $html = file_get_contents("http://nw4.novaworld.net/bhd_3_list.lob?page=0")
  5.  
  6. $regEx = '\{a\.s\.u\}'
  7.  
  8. if(preg_match($regEx, $html, $matches))
  9. {
  10.     echo "<img src="http://asu-clan.info/html/serverstatus/online.bmp">Server Online!"
  11. }
  12. else
  13. {
  14.     echo "<img src="http://asu-clan.info/html/serverstatus/offline.bmp">Server Offline"
  15. }
  16. ?>
  17.  
Sorry to bother with another question, but as I said, the page I'm trying to get info from sometimes consists of multiple pages (1 or 2 more), then it looks like this:
http://nw4.novaworld.net/bhd_3_list.lob?page=0
http://nw4.novaworld.net/bhd_3_list.lob?page=1
http://nw4.novaworld.net/bhd_3_list.lob?page=2

Is it possible to use a star ( * ) at the pagenumber ? ( page=* ) Will this make it start from page 0 to page 99999999 till it found ' {a.s.u} ', or is there a better way ?
Aug 2 '08 #6
I also seem to be getting an error with the above code:

Parse error: syntax error, unexpected T_VARIABLE in /home/rsksquad/public_html/html/blocks/block-serverstatus.php on line 6

I've tried some things such as changing the regEx into one single letter but it just doesnt seem to work


EDIT:

I fixed this error now, but now it says:

unexpected T_ELSE in /home/rsksquad/public_html/html/blocks/block-serverstatus.php on line 10

Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3. $html = file_get_contents("http://nw4.novaworld.net/bhd_3_list.lob?page=0");
  4.  
  5. $regEx = 'a\.s\.u SAS';
  6.  
  7. if (preg_match($regEx, $html, $matches)); {
  8.     echo "<img src=http://asu-clan.info/html/serverstatus/online.bmp>Server Online!";
  9. }
  10. else {
  11.     echo "<img src=http://asu-clan.info/html/serverstatus/offline.bmp>Server Offline";
  12. }
  13. ?>
  14.  
Aug 3 '08 #7
I cannot seem to edit my latest post, Im sorry to reply again :(

I now have this code:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3. $html = file_get_contents("http://nw4.novaworld.net/bhd_3_list.lob?page=0");
  4.  
  5. $regEx = 'a\.s\.u SAS';
  6. if (preg_match_all($regEx, $html, $matches)) {
  7.     print_r($matches);
  8.     print "a.s.u SAS Server";
  9.     print "<br>";
  10.     print "<img src=http://asu-clan.info/html/serverstatus/online.bmp>Server Online!";
  11. } else {
  12.     print "a.s.u SAS Server";
  13.     print "<br>";
  14.     print "<img src=http://asu-clan.info/html/serverstatus/offline.bmp>Server Offline";
  15. }
  16.  
  17. ?>
  18.  
  19.  
  20.  
  21. <?php
  22.  
  23. $html = file_get_contents("http://nw4.novaworld.net/bhd_3_list.lob?page=0");
  24.  
  25. $regEx = '\{a\.s\.u\}';
  26. if (preg_match_all($regEx, $html, $matches)) {
  27.     print "a.s.u Sniper Server";
  28.     print "<br>";
  29.     print "<img src=http://asu-clan.info/html/serverstatus/online.bmp>Server Online!";
  30. } else {
  31.     print "a.s.u Sniper Server";
  32.     print "<br>";
  33.     print "<img src=http://asu-clan.info/html/serverstatus/offline.bmp>Server Offline";
  34. }
  35.  
  36. ?>
  37.  
I still dont know how I can make it read more sites than just one, I tried several things but they only resulted in errors.

With my current code, I seem to get these errors:

Warning: preg_match_all() [function.preg-match-all]: Delimiter must not be alphanumeric or backslash in /home/rsksquad/public_html/html/blocks/block-serverstatus.php on line 6

Warning: preg_match_all() [function.preg-match-all]: Delimiter must not be alphanumeric or backslash in /home/rsksquad/public_html/html/blocks/block-serverstatus.php on line 23
Aug 3 '08 #8
Resolved, thanks for help though Atli.
Aug 6 '08 #9

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

1 post views Thread by Terry Mulvany | last post: by
3 posts views Thread by psujkov | last post: by
4 posts views Thread by =?Utf-8?B?anBzaG9ydHN0dWZm?= | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.