Author: Armel Fauveau
Original address: Http://www.phpbuilder.net/columns/armel20010427.php3
PHP can open remote or local server sockets! Here is a simple example of using a socket: Connect to Usenet news server, communicate with the server, and download some articles from an accurate news group.
Open the socket using PHP
Use Fsockopen () to open a socket. This function is present in both PHP3 and PHP4. The prototype of the function is as follows:
Intfsockopen
(String hostname,
int port [,
int errno [,
String Errstr [,
Double timeout]])
?>
For a network host, it will establish a TCP socket attached to the host name on the port. The host name can be a domain name or an IP address. For UDP connections, you need to explicitly state their protocol: Udp://hostname. For UNIX hosts, the hostname is used in the path of the socket, in which case the port must be set to 0. Optional timeout can be used to set the number of seconds the connection time-out.
For more information about Fsockopen (), you can access http://www.php.net/manual/function.fsockopen.php
Network News Transfer Protocol (NNTP)
Accessing a Usenet news server requires a special protocol called NNTP, the Network News Transfer Protocol standard. The details of this agreement are in RFC977, which you can look up in the http://www.w3.org/Protocols/rfc977/rfc977.html. This document describes in detail how to use different commands to connect and talk to the NNTP server.
Connecting to a server
Connecting to an NNTP server requires knowing the host name (or IP address) of the server and the port it will listen to. It is also recommended that you add a time-out, so that when the connection fails, it will not "freeze" the program.
$cfgServer = "Your.news.host";
$cfgPort = 119;
$cfgTimeOut = 10;
Open Asocket
if (! $cfgTimeOut)
Without timeout
$usenet _handle = Fsockopen ($cfgServer, $cfgPort);
Else
With timeout
$usenet _handle = Fsockopen ($cfgServer, $cfgPort, & $errno, & $errstr, $cfgTimeOut);
if (! $usenet _handle) {
echo "connexionfailed\n";
Exit ();
}
else {
echo "connected\n";
$tmp = fgets ($usenet _handle, 1024);
}
?>
Interacting with the server
Now that we are connected to the server, we are able to interact with the server through a previously opened socket connection. Let's say to the server, "we're going to get the latest 10 articles from a news group." RFC977 defines how to select the correct news grouping command, as follows:
Groupggg
The required parameter GGG is the name of the news Group you will choose, such as Net.news. You can get a list of valid news lists using the List command. A successful selection response returns the news number of the two news in the group and an estimate of the archived news number.
Like what
chrome:~$ Telnetmy.news.host 119
Trying aa.bb.cc.dd ...
Connected Tomy.news.host.
Escape character is ' ^] '.
My.news.hostInterNetNews NNRP Server INN 2.2.2 13-dec-1999 ready (posting OK).
GROUP Alt.test
211 232 222996 223235alt.test
Quit
205.
On receiving the command "GROUP alt.test", the news server returned "211232 222996 223235 alt.test". Where 211 is the RFC identifier (simple explanation that the command has been executed successfully-view the RFC you can get more detailed information), and return the information stating that there are 232 articles, of which the oldest news index number is 222996, and the latest news index number is 223235. Now let's calculate: 222996+232 is not equal to 232235. The lost article was either removed from the server or canceled by his author (yes, it is possible and easy to implement), or deleted.
As a precaution, the server may need authentication before selecting a newsgroup, which is, of course, determined by whether the server is public or private. It is generally permissible for anyone to get news, but publishing the news requires certification.
$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! = "281ok\r\n") {
echo "502Authentication error\n";
Exit ();
}
}
Select newsgroup
Fputs ($usenet _handle, "GROUP". $cfgNewsGroup. " \ n ");
$tmp = fgets ($usenet _handle, 1024);
if ($tmp = = "480 Authentication required for command\r\n") {
echo "$tmp \ n";
Exit ();
}
$info = Split ("", $tmp);
$first = $info [2];
$last = $info [3];
Print "First: $first \ n";
Print "Last: $last \ n";
?>
Get some articles
Now that we have a index number for the latest article, it's easy to get the latest 10 articles. RFC977 points out that using the article command can be used with the index number of the article or the ID of the message. For the sake of caution, here, the index number and message ID of the article are different, because each news server definition is different, so the index number of the same article on a different news server will not be the same, but the message ID is good is unique (included in the head of the article)
$cfgLimit = 10;
Upload last Articles
$boucle = $last-$cfgLimit;
while ($boucle <= $last) {
Set_time_limit (0);
Fputs ($usenet _handle, "article$boucle\n");
$article = "";
$tmp = fgets ($usenet _handle, 4096);
if (substr ($tmp, 0, 3)! = "220") {
echo "+----------------------+\n";
echo "Error onarticle $boucle \ n";
echo "+----------------------+\n";
}
else {
while ($tmp! = ". \ r \ n") {
$tmp = fgets ($usenet _handle, 4096);
$article = $article. $tmp;
}
echo "+----------------------+\n";
echo "article$boucle\n";
echo "+----------------------+\n";
echo "$article \ n";
}
$boucle + +;
}
?>
We only get 10 of the latest news from this server's group. You can also use the Head command to get header information for an article, or use the body command to get the body of the news.
Close connection
With the fclose () function You can end the session with the NNTP server, and of course you can have a new file, as follows:
Close Connexion
Fclose ($usenet _handle);
?>
For more information about fclose (), see: http://www.php.net/manual/function.fclose.php
Conclusion
In this article, we only show you how to open, use, and close a socket connection when you are sure: Connect to the previous NNTP server and retrieve some articles from the news group. Using the post command to publish an article on an NNTP server is not much more complicated.
So, the next step is to write a news client (and get rid of some Netscape), it needs to be able to save the article easily, and use some search engines (such as Htgid, http://www.htdig.org/) to index these articles, And there's a Web application that can search for keywords under news groupings. Here's an example where you can visit http://www.phpindex.com/ng/to download.
http://www.bkjia.com/PHPjc/318518.html www.bkjia.com true http://www.bkjia.com/PHPjc/318518.html techarticle Author: Armelfauveau original address: http://www.phpbuilder.net/columns/armel20010427.php3 PHP can open a remote or local server sockets! Here is a simple use of the socket ...