Use sockets in PHP: Get articles from newsgroups _php

Source: Internet
Author: User
Keywords get article news use server one Usenet_han
Tags nntp nntp server
PHP can open a socket port on a remote or local host. This article is a small example of using a socket: even
Get 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. function declaration
That is true:
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
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 path to the socket, in which case port ports must be set to 0. Options available
The timeout parameter is used to set the time, in seconds, to wait for the opening of a socket.
For more information about Fsockopen (), please refer to: H
ttp://www.php.net/manual/function.fsockopen.php

Network News Transfer Protocol
Accessing a newsgroup server requires a protocol called NNTP (Network News Transfer Protocol). This Agreement
Detailed details in the rfc977 can be found in the http://www.w3.org/Protocols/rfc977/rfc977.html
Get. This document describes how to connect to the NNTP server, how to talk to the server, and complete the
Different commands for some tasks.

Connection
Connecting to an NNTP server requires knowing its hostname (or IP address) and the port it listens to. In order to
To avoid a connection attempt failure that causes the program to hang, you should use the timeout parameter.
$cfgServer = "Your.news.host";
$cfgPort = 119;
$cfgTimeOut = 10;

Open a socket
if (! $cfgTimeOut)
Without timeout
$usenet _handle = Fsockopen ($cfgServer, $cfgPort);
Else
With timeout
$usenet _handle = Fsockopen ($cfgServer, $cfgPort, & $errno, & $errstr, $cfgTimeOut);

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 that was opened earlier. Like what
Say we're going to get the latest 10 articles from a newsgroup. RFC977 points out that the first step is to use the group command to select
The Right newsgroups:
GROUP GGG
The parameter GGG is the name of the newsgroup to select (for example, "Net.news"), which is required. The available new
The list of the sniffing groups can be obtained by using List command. When you select a newsgroup command successfully, the first and most
The article number for the latter article, and the number of articles in the group.
Here is an example:
chrome:~$ Telnet My.news.host 119
Trying aa.bb.cc.dd ...
Connected to My.news.host.
Escape character is ' ^] '.
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 is the return code defined in the RFC indicating that the command was executed successfully. The return information also states that there are now 232 articles
Chapter, the earliest article number is 222996, the latest article number is 223235. We see that
222996+232 is not equal to 223235. The 7 lost articles were removed from the server for some reason and may
Because it was canceled by its rightful author (it's possible, and it's easy to do), or because it's a text
The chapter and is deleted.
It is important to note that some servers may require authentication before selecting newsgroups, depending on whether this is a
A public or private server. It is also possible that the server allows anyone to read the article, but publishing the article requires
Authentication.


$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! = "281 okrn") {
echo "502 Authentication Errorn";
Exit ();
}
}

Select newsgroup

Fput ($usenet _handle, "GROUP". $cfgNewsGroup. " 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);

?>
  • Related Article

    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.