Starting out with PHP sockets

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
User avatar
Joe
Forum Regular
Posts: 939
Joined: Sun Feb 29, 2004 1:26 pm
Location: UK - Glasgow

Starting out with PHP sockets

Post 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)
User avatar
Joe
Forum Regular
Posts: 939
Joined: Sun Feb 29, 2004 1:26 pm
Location: UK - Glasgow

Post 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!
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Code: Select all

while($lines[] = @fgets($Source));
? stab in the dark, there... :)
User avatar
dull1554
Forum Regular
Posts: 680
Joined: Sat Nov 22, 2003 11:26 am
Location: 42:21:35.359N, 76:02:20.688W

Post 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
Post Reply