View Full Version : Socket stiff and more...
SneakyDave
09-22-1999, 01:38 PM
Does anybody know where I can go on the web (or book) that I can learn about socket processing, specifically in PHP?
Thanks
Justin
09-22-1999, 02:45 PM
Hm... The only way I figured out using sockets w/php was by experimentation...
Basically, you can specify a hostname, port, and timeout value for a socket connection (assuming you are refering to fsockopen())... The other two parameters required for fsockopen() are two variables that are passed by reference - ie, the function modifies these variables. The first will be set to the error number, and the second to the error description string.
Example:
$FP = fsockopen ("hostfacts.com", 80, $junk, $stuff, 18);
This opens a connection on port 80 - note that you do not put the protocol with the host name (http://), as the port decides this (it's a more direct method). Also note that the fopen() problems from previous versions (3.0.9 through 3.0.11) did not affect fsockopen(), at least in my experience.
Now $FP is set to either false for error or a file pointer. You can then use something like this:
fputs ($FP, "GET /\r\n\r\n");
Then you can retrieve the page like this:
while (!feof ($FP)) {
[nbsp][nbsp] print (fgets ($FP, 4096));
}
fclose ($FP);
Once you learn about the different ports, protocols, and specifications, you can do other things, such as making an FTP connection, sending/receiving email, and more - I've been experimenting with the receiving email portion using PHP, and so far so good (see RFC 1460 for the commands you'd need to know, remembering that each command is always followed by a CRLF combo (\r\n)).
Hope this helps - I wish I knew of some tutorials, but I don't... I will say that PHP's fsockopen() and other socket related functions are pretty direct, allowing you to directly interface with a socket - thus, there are no limitations within PHP other than the max execution time...
------------------
Justin Nelson
FutureQuest Support
SneakyDave
09-23-1999, 05:21 PM
Yeah, I tried that stuff with a lot of success Justin. Specifically, I'm looking into a POST operation (I think).
Let's say that there is a form page located at:
http://www.justinsserver.com/search/form/findit.cgi
And its just a text box with a submit button to search a forum.
My fopen would look something like:
$FP = fsockopen ("www.justinsserver.com (http://www.justinsserver.com)", 80, "search/form/", $stuff, 30);
Just some questions:
Would the path name variable include the cgi routine name?
What would be the "stuff" that I'd put into $stuff?
I've looked around PHP.net and other places and haven't found anything that looks like something I could work with.
I'll play around with it some more and see if I can come up with some answers.
Thanks for your time, Justin.[nbsp]
ChrisH
09-23-1999, 05:57 PM
You have to look into the HTTP protocol if you are
doing the raw socket stuff. For a GET, maybe something
like this:
GET /search/form?foo=bar http/1.0
or something like that. You have to take care
of the headers and also the content. If the form
will take it, GET may be easier. POST will
be better for more info, though.
One way to test this is to telnet by hand and play
around. Telnet to fq, then telnet to www.foo.com (http://www.foo.com)
on port 80:
telnet www.foo.com (http://www.foo.com) 80
Then type in the above GET line followed by two
returns (with the correct path, of course).
Doesn't PHP have some sort of URL-fetching function?
If so, you can do a GET pretty easily.
ch
Justin
09-23-1999, 07:08 PM
Your fsockopen() is not correct... The parameters are as follows:
int fsockopen(string hostname, int port, int [errno], string [errstr], double [timeout]);
Note that errno and errstr are variables that you must pass by reference - if you aren't familiar with the term, this means that you pass the variable name to the function, and that variable is modified directly. For example,
$FP = fsockopen ("www.foo.com (http://www.foo.com)", 80, $ErrNum, $ErrStr, 10);
If an error occurrs, $ErrNum will be set with the error number, and $ErrStr would be set with the error description.
Once you have made a connection (eg, $FP is set to a non-zero value), you have to send headers to the server, followed by a pair of CRLF's:
fputs ($FP, "GET /foo/bar.html\r\n\r\n");
You can of course use POST or PUT methods, but you will need to read up on how exactly these work - I only recently learned how to parse a file upload (raw, through Perl w/o using any modules).
And of course you'll want to close the connection when you're done.
Chris - yes, PHP does have URL fetching functions, but these were broken from version 3.0.9 to 3.0.11... on top of that, PHP sends a user agent of PHP/3.0.12, which is hard coded in the source code - but doing a direct socket connection you can send any UserAgent you wish (SneakySpider/1.0 :))
Hope this helps.
------------------
Justin Nelson
FutureQuest Support
SneakyDave
09-24-1999, 02:18 PM
Thanks guys, I'll go looking for some more information on the task at hand.
And by the way, the title of the thread was supposed to be "Sockets and stuff", not "Sockets and stiff"!
vBulletin® v3.6.8, Copyright ©2000-2013, Jelsoft Enterprises Ltd.