Socket Question

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Whear
Forum Newbie
Posts: 5
Joined: Tue Jan 20, 2009 5:30 pm

Socket Question

Post by Whear »

Was trying to access a game website to retrieve data from a page. I can't seem to manage to get the socket to load the data from a web page. Thanks for your help in advance.

Code: Select all

<?php
 
$host = "hiscore.runescape.com";
$fp = fsockopen($host, 80, $errno, $errdesc) or
die("Connection to $host failed");
 
    $out = "GET / HTTP/1.1\r\n";
    $out .= "Host: hiscore.runescape.com\r\n";
    $out .= "Connection: Close\r\n\r\n";
 
$page = array();
fputs($fp, $out);
while(!feof($fp)){
$page = fgets($fp, 1024);
}
 
fclose($fp);
echo "The server returned ".(count($page)).
" Lines";
 
for($i=0; $i < count($page); $i++){
echo $page[$i];
}
 
?>
User avatar
it2051229
Forum Contributor
Posts: 312
Joined: Tue Dec 25, 2007 8:34 pm

Re: Socket Question

Post by it2051229 »

unless the server is listening to socket port 80.... wait ... HTTP is for 80...or the server doesn't listen to socket connections
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Re: Socket Question

Post by Burrito »

change:
$page = fgets($fp, 1024);
to:
$page[] = fgets($fp, 1024);

inside your while loop.
Whear
Forum Newbie
Posts: 5
Joined: Tue Jan 20, 2009 5:30 pm

Re: Socket Question

Post by Whear »

I looked at the code of another program similar to what I wanted and found my mistake (although you two helped with toher not related problems).

Anyways, my issue was with the lines 7-9, they should of been:

Code: Select all

   $out = "GET /index_lite.ws?player=zezima HTTP/1.1\r\n";
    $out .= "Host: hiscore.runescape.com\r\n";
    $out .= "Connection: Close\r\n\r\n";
Post Reply