Use sockets to get an article from a newsgroup _php tutorial

Source: Internet
Author: User
Tags nntp nntp server
By Armel Fauveau a wooden translation
PHP can open a socket port on a remote or local host. This article is a small example of using a 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 this:
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. The 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 path to the socket, in which case port ports must be set to 0. The optional 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: http://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 protocol has detailed details in the rfc977, which can be obtained in 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 for accomplishing these tasks.
Connection
Connecting to an NNTP server requires knowing its hostname (or IP address) and the port it listens 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.";
Exit ();
}
else {
echo "Connected.";
$tmp = fgets ($usenet _handle, 1024);
}
?>

http://www.bkjia.com/PHPjc/531742.html www.bkjia.com true http://www.bkjia.com/PHPjc/531742.html techarticle by Armel Fauveau PHP can open a socket port on a remote or local host. This article is a small example of using a socket: Connect to a Usenet newsgroup server, talk to a server ...

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