Use sockets to get files from Usenet in php _php tips

Source: Internet
Author: User
Tags nntp nntp server rfc
Author: Armel Fauveau
Original address: Http://www.phpbuilder.net/columns/armel20010427.php3
PHP can open a remote or local server sockets! Here is a simple example of using sockets: Connect to a Usenet news server, communicate with the server, and download some articles from an accurate newsgroup.

Open the socket with PHP
Use Fsockopen () to open a socket. This function is present in both PHP3 and PHP4. The prototype of the function is as follows:
<?php

Intfsockopen
(String hostname,
int port [,
int errno [,
String Errstr [,
Double timeout]]]
?>
For a network host, it will establish a TCP socket connected to the host name of the port. The host name can be a domain name or an IP address. For UDP connections, you need to explicitly indicate their protocol: Udp://hostname. For UNIX hosts, the host name is used in the socket path, in which case the port must be set to 0. Optional timeout can be used to set the number of seconds the connection times out.
More information about Fsockopen () can be accessed by 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 and you can see it in 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 timeout so that the connection does not "freeze" the program when it fails.
<?php
$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 open socket connection. Let's say to the server, "we're going to get the latest 10 articles from a newsgroup." RFC977 defines the command to select the correct newsgroup, as follows:
Groupggg
Required parameter GGG is the name of the newsgroup you are going to select, such as Net.news. Using the list command you can get a list of valid news lists. A successful selection response returns the news number of the two-story 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.
After receiving the command "GROUP alt.test", the news server returned "211232 222996 223235 alt.test". 211 is the RFC identification code (simple explanation that the command has been successfully executed-view the RFC you can get more detailed information), the return information to show that there are 232 articles, 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, it is easy to implement), or it is deleted.
Be careful, the server may need to authenticate before selecting a newsgroup, but it is determined by whether the server is public or private. It generally allows anyone to get the news, but publishing the news needs to be authenticated.
<?php
$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 = = "Authentication required for command\r\n") {
echo "$tmp \ n";
Exit ();
}
$info = Split ("", $tmp);
$first = $info [2];
$last = $info [3];

Print "A: $first \ n";
Print "Last: $last \ n";
?>

Get some articles
Now that we have an index number for the latest article, it's easy to get the latest 10 articles. RFC977 that the article command can be used with the index number of the article or the ID of the message. To be careful, here, the index number of the article and the message ID are different because each news server definition is different, so the index number of the same article on the different news server will vary, but the message ID is a good one (included in the header of the article)
<?php
$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've only got 10 of the latest news from this group of servers. You can also use the Head command to get the header information for the article, or use the body command to get the text of the news.

Close connection
Using the fclose () function You can end the session with the NNTP server, and of course you can have a new file as follows:
<?php
Close Connexion
Fclose ($usenet _handle);
?>
For more information on fclose (), please see: http://www.php.net/manual/function.fclose.php

Conclusion
In this article, we only explain how to open, use, and close a socket connection in a certain situation: Connect to the previous NNTP server and retrieve some articles from the newsgroup. It is not much more complicated to publish an article on an NNTP server using the Post command.
So the next step is to write a news client (and remove some Netscape), it needs to be easy to save the article and use some search engines (such as Htgid, http://www.htdig.org/) to index the articles, And there's a Web application that can do keyword searches under newsgroups. Here's an example where you can visit http://www.phpindex.com/ng/to download.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.