Use sockets in PHP: Get articles from newsgroups

Source: Internet
Author: User
Tags nntp nntp server

PHP can open socket ports on a remote or local host. This article is a small example of using Socket: connect to a USENET newsgroup server, talk to the server, and download someArticle.

Open a socket in PHP
Use fsockopen () to open a socket. This function can be used in both php3 and PhP4. The function declaration is as follows:

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

This function will open a TCP connection to the port of the host hostname. Hostname can be a valid domain name or an IP address. For UDP connections, you must specify the Protocol: UDP: // hostname. For Unix domains, the host name uses the socket path. In this case, the port must be set to 0. The optional timeout parameter is used to set the waiting time for opening a socket, in seconds.

For more information about fsockopen (), see: http://www.php.net/manual/function.fsockopen.php

Network news transmission protocol
To access a newsgroup server, you must use a protocol called NNTP (network news transmission protocol. This Protocol has detailed details in rfc977, which can be obtained at http://www.w3.org/protocols/rfc977/rfc977.html. This document describes how to connect to the NNTP server, how to talk to the server, and the different commands used to complete these tasks.

Connection
To connect to an NNTP server, you need to know its host name (or IP address) and the port on which it listens. To avoid a connection attempt failureProgramPending. You should use the timeout parameter.
<? PHP
$ Monitoring Server = "Your. News. Host ";
$ Export port = 119;
$ Required timeout = 10;

// Open a socket
If (! $ Timeout)
// Without timeout
$ Usenet_handle = fsockopen ($ container server, $ Container Port );
Else
// With timeout
$ Usenet_handle = fsockopen ($ response server, $ Response port, & $ errno, & $ errstr, $ response timeout );

If (! $ Usenet_handle ){
Echo "connection failed. \ n ";
Exit ();
}
Else {
Echo "connected. \ n ";
$ TMP = fgets ($ usenet_handle, 1024 );
}

?>
Dialog with server

Now that we have connected to the server, we can talk to the server through the socket opened previously. For example, we want to get the last 10 articles from a news group. Rfc977 indicates that the first step is to use the group command to select the correct newsgroup:
Group ggg
The ggg parameter is the name of the newsgroup to be selected (for example, "net. News"), which is required. You can use the LIST command to obtain the list of available newsgroups. After the newsgroup command is selected, the number of the first and last articles in the group and the number of articles in the group are returned.

The following is an example:
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
211 232 222996 223235 alt. Test
Quit
205.

After receiving the Command Group alt. Test, the server returns "211 232 222996 223235 alt. Test". 211, which is the return code defined in RFC, indicating that the command has been successfully executed. The returned information also indicates that there are currently 232 articles. The first article number is 222996, and the latest article number is 223235. We can see that 222996 + 232 is not equal to 223235. The 7 Lost articles were deleted from the server for some reason, probably because they were canceled by its legal author (this is possible and easy to do ), or, it is deleted because it is a sprinkling article.

Note that some servers may require authentication before selecting a newsgroup, depending on whether it is a public or private server. It is also possible that the server allows anyone to read articles, but the post requires authentication.

<? PHP

// $ Login User = "xxxxxx ";
// $ Cfgpasswd = "yyyyyy ";
$ Optional newsgroup = "alt. php ";

// Identification required on private server
If ($ Login User ){
Fputs ($ usenet_handle, "authinfo user". $ your user. "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

Fput ($ usenet_handle, "group". $ using newsgroup. "N ");
$ TMP = fgets ($ usenet_handle, 1024 );

If ($ TMP = "480 authentication required for commandrn "){
Echo $ TMP;
Exit ();
}

$ Info = Split ("", $ TMP );
$ First = $ info [2];
$ Last = $ info [3];

Printf ("First: % Sn", $ first );
Printf ("last: % lastn", $ last );

?>

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.