Get Files in PHP fsockopen () us.net
Open socket
Use Fsockopen () to open a socket. This function is present in both PHP3 and PHP4. The prototype of the function is as follows: <?php
Intfsockopen
(String hostname,
int port [,
int errno [,
String Errstr [,
Double timeout]]]
?> for a network host, it will establish a TCP socket connected to the host name of the port. The host name can be a domain name or an IP address. For UDP connections, you need to explicitly indicate their protocol: Udp://hostname. For UNIX hosts, the host name is used in the socket path, in which case the port must be set to 0. Optional timeout can be used to set the number of seconds the connection times out. For more information about Fsockopen () access to http://www.php.net/manual/function.fsockopen.php Network News Transfer Protocol (NNTP) access to a Usenet news server requires a special protocol , called NNTP, is the Network News Transfer Protocol standard. The details of this agreement are in RFC977 and you can see it in http://www.w3.org/Protocols/rfc977/rfc977.html. This document describes in detail how to use different commands to connect and talk to the NNTP server. Connecting a server to an NNTP server requires knowing the hostname (or IP address) of the server and the port it will listen to. It is also recommended that you add a timeout so that the connection does not "freeze" the program when it fails. <?php
$cfgServer = "Your.news.host";
$cfgPort = 119;
$cfgTimeOut = 10;
Open Asocket
if (! $cfgTimeOut)
Without timeout
$usenet _handle = Fsockopen ($cfgServer, $cfgPort);
Else
With timeout
$usenet _handle = Fsockopen ($cfgServer, $cfgPort, & $errno, & $errstr, $cfgTimeOut);
if (! $usenet _handle) {
echo "Connexionfailedn";
Exit ();
}
else {
echo "Connectedn";
$tmp = fgets ($usenet _handle, 1024);
}
?> with the server now that we are connected to the server, we are able to interact with the server through a previously open socket connection. Let's say to the server, "we're going to get the latest 10 articles from a newsgroup." RFC977 defines how to select the correct newsgroup command, as follows: Groupggg required parameter GGG is the name of the newsgroup you are going to select, such as Net.news. Using the list command you can get a list of valid news lists. A successful selection response returns the news number of the two-story news in the group and an estimate of the archived news number. For example, chrome:~$ telnetmy.news.host 119Trying aa.bb.cc.dd ... Connected tomy.news.host.Escape character is ' ^] ' my.news.hostInterNetNews NNRP server INN 2.2.2 13-dec-1999 Ready (PO Sting OK). Group alt.test211 232 222996 223235alt.testquit205. After receiving the command "GROUP alt.test", the news server returned the 211232 222996 223235. 211 is the RFC identification code (simple explanation that the command has been successfully executed-view the RFC you can get more detailed information), the return information to show that there are 232 articles, the oldest news index number is 222996, and the latest news index number is 223235. Now let's calculate: 222996+232 is not equal to 232235. The lost article was either removed from the server or canceled by his author (yes, it is possible, it is easy to implement), or it is deleted. Be careful, the server may need to authenticate before selecting a newsgroup, but it is determined by whether the server is public or private. It generally allows anyone to get the news, but publishing the news needs to be authenticated. <?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!= "281Okrn") {
echo "502Authentication Errorn";
Exit ();
}
}
Select newsgroup
Fputs ($usenet _handle, "GROUP". $cfgNewsGroup. " n ");
$tmp = fgets ($usenet _handle, 1024);
if ($tmp = = "Authentication required for Commandrn") {
echo "$TMPN";
Exit ();
}
$info = Split ("", $tmp);
$first = $info [2];
$last = $info [3];
Print "A: $FIRSTN";
Print "Last: $lastn";
?> get some articles now that we have an index number for the latest article, it's easy to get the latest 10 articles. RFC977 that the article command can be used with the index number of the article or the ID of the message. To be careful, the index number and message ID of the article are different here, because each news server definition is different, so the index number of the same article on the different news server will vary, but the message ID is a good one (included in the header of the article) <?php
$cfgLimit = 10;
Upload last Articles
$boucle = $last-$cfgLimit;
while ($boucle <= $last) {
Set_time_limit (0);
Fputs ($usenet _handle, "Article$bouclen");
$article = "";
$tmp = fgets ($usenet _handle, 4096);
if (substr ($tmp, 0, 3)!= "220") {
echo "+----------------------+n";
echo "Error onarticle $bouclen";
echo "+----------------------+n";
}
else {
while ($tmp!= ". Rn") {
$tmp = fgets ($usenet _handle, 4096);
$article = $article. $tmp;
}
echo "+----------------------+n";
echo "Article$bouclen";
echo "+----------------------+n";
echo "$articlen";
}
$boucle + +;
}
?>