Use sockets in php: getting articles from newsgroups _ PHP Tutorial-php Tutorial

Source: Internet
Author: User
Tags nntp nntp server
Use sockets in php: get articles from newsgroups. 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 and talk to the server. download a PHP from the newsgroup to open the Socket port 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 some articles from the newsgroup.


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 program suspension caused by a connection attempt failure, you should use the timeout parameter.
$ 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.


// $ 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 );

?>

Bytes. This article is a small example of using Socket: connect to a Usenet newsgroup server, talk to the server, download one from the newsgroup...

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.