Home >Backend Development >PHP Tutorial >Using sockets in php: Get articles from newsgroups

Using sockets in php: Get articles from newsgroups

高洛峰
高洛峰Original
2016-12-01 15:10:201107browse

PHP can open Socket ports on remote or local hosts. This article is a small example of using Socket: connect to a Usenet news group server, talk to the server, and download some articles from the news group.


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 like this:


int fsockopen (string hostname, int port _
[, int errno [, string errstr [, double timeout]]])


This function will open a port connected to the host hostname TCP connection. 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 hostname uses the path to the socket. In this case, port must be set to 0. The optional timeout parameter is used to set the time to wait to open a socket, in seconds.


Network News Transfer Protocol
Accessing newsgroup servers requires a protocol called NNTP (Network News Transfer Protocol). This document describes how to connect to an NNTP server, how to talk to the server, and the different commands to accomplish these tasks.


Connect
Connecting to an NNTP server requires knowing its host name (or IP address) and the port it is listening on. To avoid a failed connection attempt causing the program to hang, you should use the timeout parameter.
                                                                            $cfgServer                                                                                                                                          $cfg   
                   // without timeout                                                                      usenet_handle = fsockopen($cfgServer, $cfgPort);
                                                                                 ’ ’s ’ ’               ‐ ‐   ‐   ‐ ‐‐‐‐‐ with timeout                                                        usenet_handle) {
                                                                                                                             exit(); ;

      }


?>
With the server Dialogue

Now that we have connected to the server, we can talk to the server through the socket opened earlier. For example, let's say we want to get the last 10 articles from a certain news group. RFC977 points out that the first step is to use the GROUP command to select the correct news group:
GROUP ggg
The parameter ggg is the name of the news group to be selected (for example, "net.news"), which is required. A list of available newsgroups can be obtained with the LIST command. When the command to select a newsgroup succeeds, it returns the article numbers of the first and last articles in the group, as well as the number of articles in the group.


Here is an example:
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 .

Received the command GROUP alt After .test, the server returns "211 232 222996 223235 alt.test". 211 is the return code defined in the RFC, indicating that the command was successfully executed. The return information also points out that there are now 232 articles, the earliest article number is 222996, and the latest article number is 223235. We see that 222996+232 is not equal to 223235. The missing 7 articles were deleted from the server for some reason, either because they were canceled by their legitimate authors (which is possible and easy to do), or because they were spam articles.


Things to note, some servers may require authentication before selecting a news group, depending on whether it is a public or private server. It's also possible that the server allows anyone to read the article, but publishing the article requires authentication.

           
      //$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);

?>

 


Statement:
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