Using Sockets in PHP: Getting files from Usenet _php tutorial

Source: Internet
Author: User
Tags nntp nntp server rfc

Author: Armel Fauveau
Original address: Http://www.phpbuilder.net/columns/armel20010427.php3
Translator: Liqiang feifengxlq@gmail.com
Http://www.phpobject.net/blog/



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 can be found in the HTML ">http://www.w3.org/protocols/rfc977/rfc977.html" in RFC977. 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";
Exit ();
}
else {
echo "Connected";
$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. ");
$tmp =fgets ($usenet _handle,1024);
Fputs ($usenet _handle, "Authinfopass". $cfgPasswd. ");
$tmp =fgets ($usenet _handle,1024);
Check error
if ($tmp! = "281Ok") {
echo "502Authentication error";
Exit ();
}
}
Select newsgroup
Fputs ($usenet _handle, "GROUP". $cfgNewsGroup. ");
$tmp =fgets ($usenet _handle,1024);
if ($tmp = = "480 Authentication required for command") {
echo "$tmp";
Exit ();
}
$info =split ("", $tmp);
$first = $info [2];
$last = $info [3];

Print "First: $first";
Print "Last: $last";
?>

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");
$article = "";
$tmp =fgets ($usenet _handle,4096);
if (substr ($tmp, 0, 3)! = "220") {
echo "+----------------------+";
echo "Error onarticle $boucle";
echo "+----------------------+";
}
else {
while ($tmp! = ".") {
$tmp =fgets ($usenet _handle,4096);
$article = $article. $tmp;
}
echo "+----------------------+";
echo "Article$boucle";
echo "+----------------------+";
echo "$article";
}
$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 the 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/508233.html www.bkjia.com true http://www.bkjia.com/PHPjc/508233.html techarticle Author: Armel Fauveau original address: http://www.phpbuilder.net/columns/armel20010427.php3 Translator: Liqiang feifengxlq@gmail.com/http Www.phpobject.net/blog/PHP can open remote ...

  • 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.