17.5. Reading Usenet News Messages
Problem
You want to read Usenet news messages using NNTP to talk to a news server.
Solution
Use PHP’s IMAP extension. It also speaks NNTP:
// open a connection to the nntp server
$server = '{news.php.net/nntp:119}';
$group = 'php.general'; // main PHP mailing list
$nntp = imap_open("$server$group", '', '', OP_ANONYMOUS);
// get header
$header = imap_header($nntp, $msg);
// pull out fields
$subj = $header->subject;
$from = $header->from;
$email = $from[0]->mailbox."@".$from[0]->host;
$name = $from[0]->personal;
$date = date('m/d/Y h:i A', $header->udate);
// get body
$body = nl2br(htmlspecialchars(imap_fetchbody($nntp,$msg,1)));
// close connection
imap_close($nntp);Discussion
Reading news from a news server requires you to connect to the server and specify a group you’re interested in reading:
// open a connection to the nntp server
$server = "{news.php.net/nntp:119}";
$group = "php.general";
$nntp = imap_open("$server$group",'','',OP_ANONYMOUS);The function imap_open( )
takes four parameters. The
first specifies the news server to use and the newsgroup to read. The
server here is
news.php.net
, the news server
that mirrors all the PHP mailing lists. Add /nntp
to let the IMAP extension know you’re reading news
instead of mail, and specify 119 as a port;
that’s typically the port reserved for NNTP. NNTP
stands for Network News Transport Protocol; it’s used to communicate with news servers, just as HTTP communicates with web servers. The group ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access