Page 1 of 1

Starting out with PHP sockets

Posted: Sun May 30, 2004 1:03 pm
by Joe
Since last night I decided to take on the task of socket programming in PHP. To start out I planned to attempt to grab the source of my webpage and print the contents to the screen. Its very basic I know, but it will give me a rough idea on how sockets actually communicate. So far I have this:

<?php
$host = "www.mysite.com";
$HTTP = "GET $host";
$Source = fsockopen($host,80);


echo $Source;
fclose($Source);
?>

But all i get is resource ID#2 or something along those lines. I know I have to send a GET request at port 80 in order to get the source but my method doesn't work. Any help appreciated!

Regards



Joe 8)

Posted: Sun May 30, 2004 1:16 pm
by Joe
*edit* Well I have just realised that I must connect the source variable to the HTTP variable, but im not sure how I can do that! Any help!

Posted: Sun May 30, 2004 1:30 pm
by feyd

Code: Select all

while($lines[] = @fgets($Source));
? stab in the dark, there... :)

Posted: Sun May 30, 2004 2:14 pm
by dull1554
****example straight out of the manual****

Code: Select all

<?php 
$fp = fsockopen ("www.example.com", 80, $errno, $errstr, 30); 
if (!$fp) { 
   echo "$errstr ($errno)<br>\n"; 
} else { 
   fputs ($fp, "GET / HTTP/1.0\r\nHost: http://www.example.com\r\n\r\n"); 
   while (!feof($fp)) { 
       echo fgets ($fp,128); 
   } 
   fclose ($fp); 
} 
?>
hope that helps