Home > php教程 > php手册 > body text

使用sockets:从新闻组中获取文章(三)

WBOY
Release: 2016-06-13 12:42:05
Original
795 people have browsed it

读取文章
    既然我们已经得到了最后一篇文章的文章编号,读取最后的十篇文章就是很容易的事了。RFC977中说,ARTICLE命令既可以使用文章编号,也可以使用它的消息ID.
    这里要注意,文章编号跟它的消息ID是不同的,因为每个新闻服务器分配自己的文章编号,所以同一篇文章在两个不同的服务器上的编号是不一样的,而文章的消息ID,是包括在文章的头信息里,并且是唯一的。

        $cfgLimit =10;

     //upload last articles

     $boucle = $last-$cfgLimit;

     while ($boucle       set_time_limit(0);
       fputs($usenet_handle, "ARTICLE $bouclen");

       $article="";
       $tmp=fgets($usenet_handle, 4069);
       if(substr($tmp,0,3) != "220") {
           echo "+----------------------+n";
           echo "Error on article $bouclen";
           echo "+----------------------+n";
       }
       else {
           while($tmp!=".rn") {
              $tmp = fgets($usenet_handle, 4096);
              $article .=$tmp;
           }

           echo "+----------------------+n";
           echo "Article $bouclen";
           echo "+----------------------+n";
           echo "$articlen";
       }

       $boucle++;
  }

  ?>

    我们从服务器的这个新闻组中读取了最后的十篇文章,。也可以通过使用HEAD命令读取文章的头信息,或者使用BODY命令读取文章内容。

关闭连接
    最后,通过调用fclose()函数关闭socket,结束同NNTP服务器的对话。

          //close connection
      fclose($usenet_handle);
    ?>
    关于fclose()的更多信息,请参考 http://www.php.net/manual/function.fclose.php

结论
    在上文中,我们看到了怎样打开、使用然后关闭一个socket:连接到一个NNTP服务器,取回一些文章。使用POST命令发表文章也复杂不到哪儿去。
    下一步就是编写一个基于WEB的新闻组客户端了。
    存储这些文章,用某个搜索引擎(比如说http://www.htdig.org)对文章进行索引也是非常容易的。这样,你有了一个基于web的搜索新闻组的程序了。
    这样的程序的一个例子可以在http://www.phpindex.com/ng/得到。 

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!