Use of sockets in PHP

php can open sockets by the local or removed machine. In given clause{article} the example of use of sockets will be considered for: connections with the usenet-news server, conducting dialogue with it  and uploadings of some clauses{articles}.


We open a socket.


For opening a socket function fsockopen () is used. This function is accessible, as in php3, and php4. The call of function has the following kind:

int fsockopen (string hostname, int port [, int errno [, string errstr [, double timeout]]])


For udp connections, it is necessary to define{determine} the report: udp: // hostname. It is more than information on function fsockopen () it is possible having learned{having found out} here: http://www.php.net/manual/function.fsockopen.php


The nntp-report (network news transfer protocol)


For access to the news usenet-server we shall use the nntp-report. This report is in details described in rfc977 (request for comment number 977). The full description is present at the Internet: http://www.w3.org/protocols/rfc977/rfc977.html This document in details describes procedure of connection and dialogue with the server.


Connection (connecting)


For connection with the nntp-server it is necessary for us to know his  name (or an ip-address) and port. As it is necessary to specify the timer that in case of impossibility of connection to the server "have not frozen" application.



$cfgserver = "your.news.host";

$cfgport = 119;

$cfgtimeout = 10;


// open a socket

if (! $cfgtimeout)

// without timeout

$usenet_handle = fsockopen ($cfgserver, $cfgport);

else

// with timeout

$usenet_handle = fsockopen ($cfgserver, $cfgport, both $errno, and $errstr, $cfgtimeout);


if (! $usenet_handle) {

echo " connexion failedn ";

exit ();

}

else {

echo "connectedn";

$tmp = fgets ($usenet_handle, 1024);

}


?>


Conducting dialogue with the server.


So, now we have joined the server and we can carry on dialogue with it , using earlier an open socket. For an example, we shall try to get last ten messages from any group. In rfc977 it is described, that a first step - to choose group with the help group commands:

group ggg

Obligatory parameter - ggg - a name of group which we want to choose (for example, "net.news"). The list of existing groups can be received with the help of list command. The successful choice of group will be confirmed with the answer of the server where the quantity{amount} of new, old clauses{articles} and total will be informed.



chrome: ~ $ telnet my.news.host 119

trying aa.bb.cc.dd...

connected to my.news.host.

escape character is ' ^] '.

200 my.news.host internetnews nnrp server inn 2.2.2 13-dec-1999 ready (posting ok).

group alt.test

211232222996223235 alt.test

quit

205


After reception of the command " group alt.test ", the news server has answered " 211232222996223235 alt.test ". 211 - certain{determined} rfc the specification a code (speaking usual language - 212 - means, that the command has been completed with positive result - look the documentation rfc for more full characteristic). The following figure - 232 - quantity{amount} of new clauses{articles} available in a present situation. 222996 - old. 223235 - all clauses{articles}. 232+222996 it is not equal 223235. Why? Probably, missing seven clauses{articles} have been somehow removed by the moderator or the author.


Depending on the server (public or private) you can ask to be identified. As it is possible, that identification to be necessary only at a spelling of the messages, and reading can be made without it.



// $cfguser = "xxxxxx";

// $cfgpasswd = "yyyyyy";

$cfgnewsgroup = "alt.php";


// identification required on private server

if ($cfguser) {

fputs ($usenet_handle, " authinfo user ". $cfguser. "n");

$tmp = fgets ($usenet_handle, 1024);


fputs ($usenet_handle, " authinfo pass ". $cfgpasswd. "n");

$tmp = fgets ($usenet_handle, 1024);


// check error


if ($tmp! = " 281 okrn ") {

echo " 502 authentication errorn ";

exit ();

}

}


// select newsgroup


fputs ($usenet_handle, "group." $cfgnewsgroup. "n");

$tmp = fgets ($usenet_handle, 1024);


if ($tmp == " 480 authentication required for commandrn ") {

echo "$tmpn";

exit ();

}


$info = split (" ", $tmp);

$first = $info [2];

$last = $info [3];


print " first: $firstn ";

print " last: $lastn ";

?>


Uploading of some clauses{articles}


Now we have number{room} of last clause{article} so now we cannot download last ten clauses{articles}. rfc977 the specification supposes use kommandy article, as with number{room} of clause{article}, and message id (Unique Number{Room} of the Message).


Be close{attentive} here - number{room} of clause{article} is distinct from message id. If clause{article} is published on several servers she it will be doubtless to have different number{room} both times, but identical message id. Roughly speaking, number{room} of clause{article} - is appropriated{given} each time in a new fashion on the server, and can vary in due course; message id - at each clause{article} unique.



$cfglimit = 10;


// upload last articles


$boucle = $ last-$ cfglimit;


while ($boucle <= $last) {


set_time_limit (0);


fputs ($usenet_handle, " article $bouclen ");


$article = "";

$tmp = fgets ($usenet_handle, 4096);

if (substr ($tmp, 0,3)! = "220") {

echo " +----------------------+n ";

echo " error on article $bouclen ";

echo " +----------------------+n ";

}

else {

while ($tmp! = ".rn") {

$tmp = fgets ($usenet_handle, 4096);

$article = $article. $ tmp;

}


echo " +----------------------+n ";

echo " article $bouclen ";

echo " +----------------------+n ";

echo "$articlen";

}


$boucle ++;

}


?>


As due to head command it is possible to receive only header messages or only the text, using body command.


We are disconnected from the server


To close session with the nntp-server, simply close a socket using fclose () (analagichno to closing of a file).



// close connexion


fclose ($usenet_handle);


?>


The conclusion


We just saw as to open, use and then to close a socket - for connection with the nntp-server and reception of some clauses{articles} from news groups. For publication of the message it is necessary to use post kommandu.