使用sockets:從新聞組中獲取文章(二)
2024-05-04 23:01:11
供稿:網友
與服務器對話
現在我們已經連接到了服務器,可以通過前面打開的socket同服務器對話了。比如說我們要從某個新聞組得到最近的10篇文章。rfc977指出,第一步要用group命令選擇正確的新聞組:
group ggg
參數ggg是要選擇的新聞組的名字(比如說是"net.news"),這是必需的。可用的新聞組的列表可以用list命令得到。選擇新聞組的命令成功后,返回組中第一篇和最后一篇文章的文章編號,以及組中文章的數目。
下面是一個例子:
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 .
接收到命令 group alt.test 后,服務器返回"211 232 222996 223235 alt.test". 211是rfc中定義的返回碼,指示命令已成功執行。返回信息還指出,現在有232篇文章,最早的文章的編號是222996,最新的文章的編號是223235。我們看到,222996+232并不等于223235。丟失的7篇文章因為某種原因被從服務器刪除了,可能是因為被它的合法作者取消了(這是可能的,而且很容易做到),或者因為是灌水文章而被刪。
需要注意的事,有些服務器在選擇新聞組之前可能要求身份認證,這取決于這是一個公共的或者是私用的服務器。也有可能服務器允許任何人讀取文章,但發表文章需要身份驗證。
<?php
//$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);
?>