On the “hardcores or masochists” post series i used the dumps of http, pop, smtp and imap sessions. But how did i get these? Well for the mailing ones, the answer is easy, telnet. But for the http part i created some scripts for interacting with the servers. Also, i created a php script to communicate with a pop server. I intend on giving you guys some code snippets so you will avoid all the trouble of inventing the wheel on your own, like i did.
First of all, there is the http client for making a post request on a server. The sequence is, open a socket, write the request, read the reply and hung up. Here is the code:
function makePostRequest($url_encoded_data){ $headers = "POST SCRIPT_HERE HTTP/1.1\r\n";
$headers .= "Host: HOSTNAME_HERE\r\n";
$headers .= "User-Agent: ".$_SERVER['HTTP_USER_AGENT']."\r\n";
$headers .= "Content-Type: application/x-www-form-urlencoded\r\n";
$headers .= "Content-Length: ".strlen($url_encoded_data)."\r\n";
$headers .= "Connection: close\r\n\r\n"; $send = $headers.$url_encoded_data;
$open = fsockopen(HOST_IP_HERE, PORT_HERE);
fwrite($open, $send);
$read = '';
$reply = '';
do{
$read = fgets($open);
$reply .= $read;
}while($read !== FALSE && strlen($read) != 0);
fclose($open);
return $reply;
}
A few pointers on the code. The SCRIPT_NAME actually is the path to the script of the server. And the url_encoded_data is actually the data you want to post. Here is an example of usage:
makePostRequest( "name=stratosg&gender=m&msg=Hello%20World!" )
So, this is a pretty easy task, as you can see. Moreover, one could add more headers, like the session header, or the accept-encoding. But this is the basic idea.
One more script i wrote, was to communicate with a POP server. This was a tricky one because with the pop server you keep the connection alive until you send the quit message. What this practicaly means is that on the code for HTTP the fgets (and all of it’s similars like fread) call will never return. What you will actually have there is a deadlock because the client will be waiting on server data and the server will be done and wait on a new client command.
To avoid this conflict i came up with a somewhat sloppy idea but it works like a charm. I treat the whole connection as a stream of data and use the stream_socket_client function. Then i set a timeout on the stream with the stream_set_timeout function. Essentialy what i do is i say that if no data come through for X seconds then the fread function should return. Let’s see a snippet of the initialization of the connection to the POP server.
$fd = stream_socket_client('tcp://myserver.com:110');
stream_set_timeout($fd, 10);
read_data($fd);
So here, i open a stream socket connection with the server on port 110 using tcp protocol. Then, i set the timeout for the stream to be 10 seconds and, finally, i make a call to the function read_data which follows.
function read_data($fd){
$data = '';
do{
$tmp = fread($fd, 2048);
$data .= $tmp;
}while($tmp !== FALSE && strlen($tmp) != 0); return $data;
}
As you can see, the read function is very similar to the HTTP one. This is because we avoided the deadlock with the stream and now we can safely use the fread (or even fgets if we would like) function. This way the communication with the POP server will be successful. One more pointer here. Some pop servers are slow on the process of opening a connection to them, i mean the step from the connect to the first read of data. For this reason, it might be usefull to set the timelimit to, let’s say, 30 seconds, and then after getting the hello message setting it to 1 second is, in my oppinion, absolutely safe.
All in all, the code snippets above might, i think, save you from some trouble on trial and error. For any problems leave a comment.
Thanks for the snippet. I think I saw a follow up for this at programminghelp.com
Keep up the good work!
thx man… i’ll look into that follow up you said.