Using Sockets in PHP: getting articles from newsgroups

Source: Internet
Author: User
Tags nntp nntp server socket
PHP can open a socket port on a remote or local host. This article is a small example of using a socket: even
Receive a Usenet newsgroup server, talk to the server, and download articles from newsgroups.
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 port ports connected to 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. Optional
The timeout parameter is used to set the time, in seconds, to wait for a socket to open.
For more information on fsockopen (), please refer to: H
ttp://www.php.net/manual/function.fsockopen.php
Network News Transfer Protocol
Access to a newsgroup server is required through a protocol called NNTP (Network News Transfer Protocol). This Agreement
There are detailed details in the rfc977, which can be 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 this
Different commands for some tasks.
Connection
Connecting to an NNTP server requires knowing its host name (or IP address) and the port it listens on. In order to
To avoid a connection attempt failure causing the program to hang, you should use the timeout parameter.
<?php
$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);
}
?>
Talking to the server
Now that we have connected to the server, we can talk to the server through the socket we opened earlier. Like what
Says we're going to get the last 10 articles from a newsgroup. RFC977 points out that the first step is to use the group command to select
Correct newsgroups:
GROUP GGG

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.