超越PHP PHP动态 | 经典文章 | CLASS | 相关下载 | 常见问题 | FORUM | WIKI | 在线手册
Site search:    
<dns_get_recordgethostbyaddr>
Last updated: Fri, 22 Jun 2007

fsockopen

(PHP 3, PHP 4 , PHP 5)

fsockopen --  Open Internet or Unix domain socket connection

Description

resource fsockopen ( string target, int port [, int &errno [, string &errstr [, float timeout]]])

Initiates a socket connection to the resource specified by target. PHP supports targets in the Internet and Unix domains as described in 附录 N. A list of supported transports can also be retrieved using stream_get_transports().

注: If you need to set a timeout for reading/writing data over the socket, use stream_set_timeout(), as the timeout parameter to fsockopen() only applies while connecting the socket.

As of PHP 4.3.0, if you have compiled in OpenSSL support, you may prefix the hostname with either 'ssl://' or 'tls://' to use an SSL or TLS client connection over TCP/IP to connect to the remote host.

fsockopen() returns a file pointer which may be used together with the other file functions (such as fgets(), fgetss(), fwrite(), fclose(), and feof()).

If the call fails, it will return FALSE and if the optional errno and errstr arguments are present they will be set to indicate the actual system level error that occurred in the system-level connect() call. If the value returned in errno is 0 and the function returned FALSE, it is an indication that the error occurred before the connect() call. This is most likely due to a problem initializing the socket. Note that the errno and errstr arguments will always be passed by reference.

Depending on the environment, the Unix domain or the optional connect timeout may not be available.

The socket will by default be opened in blocking mode. You can switch it to non-blocking mode by using stream_set_blocking().

例子 1. fsockopen() Example

<?php
$fp
= fsockopen("www.example.com", 80, $errno, $errstr, 30);
if (!
$fp) {
   echo
"$errstr ($errno)<br />\n";
} else {
  
$out = "GET / HTTP/1.1\r\n";
  
$out .= "Host: www.example.com\r\n";
  
$out .= "Connection: Close\r\n\r\n";

  
fwrite($fp, $out);
   while (!
feof($fp)) {
       echo
fgets($fp, 128);
   }
  
fclose($fp);
}
?>
The example below shows how to retrieve the day and time from the UDP service "daytime" (port 13) in your own machine.

例子 2. Using UDP connection

<?php
$fp
= fsockopen("udp://127.0.0.1", 13, $errno, $errstr);
if (!
$fp) {
   echo
"ERROR: $errno - $errstr<br />\n";
} else {
  
fwrite($fp, "\n");
   echo
fread($fp, 26);
  
fclose($fp);
}
?>

警告

UDP sockets will sometimes appear to have opened without an error, even if the remote host is unreachable. The error will only become apparent when you read or write data to/from the socket. The reason for this is because UDP is a "connectionless" protocol, which means that the operating system does not try to establish a link for the socket until it actually needs to send or receive data.

注: 当指定数字的 IPv6 地址(例如 fe80::1)时必须将 IP 地址放在方括号内。例如 tcp://[fe80::1]:80

注: The timeout parameter was introduced in PHP 3.0.9 and UDP support was added in PHP 4.

See also pfsockopen(), stream_set_blocking(), stream_set_timeout(), fgets(), fgetss(), fwrite(), fclose(), feof(), and the Curl extension.




add a note add a note User Contributed Notes
fsockopen
info at TAKETHISOUT dot ski-info-online dot com
08-Jan-2002 10:30
This is a "gotcha" that "got me" and discusses the careful use of HTTP/1.1 over HTTP/1.0

I had a script that suffered dreadful performance and return Hex values amongst the correct data. This was eventually traced to my inclusion of HTTP/1.1 in the line which read:

-- CODE (watch wrap) --
 $request = "GET $document" . "?" . "$query" . " HTTP/1.1\r\n";
 $request .= "Host: $myServer\r\n\r\n";
-- CODE --

By sending a HTTP/1.1 request it declares that it is willing to 'speak' HTTP/1.1, of course, but there are some aspects of HTTP/1.1 that make it necessary to handle the socket differently from HTTP/1.0.

In the RFC 2616, Section 3.6 defines:

[...]
All HTTP/1.1 applications MUST be able to receive and decode the "chunked" transfer-coding,
[...]

This was the cause of the extraneous HEX values being displayed.

Regards the loss of performance, this is also due to using HTTP/1.1, which defaults to having keepalive on until you tell it to close the connection, or it times out. Therefore the socket was being kept open by the script.

Simply changing the line in the script to HTTP/1.0 completely fixed the problem.

Speaking with one of the members in PHP-dev his words were:

[Comment from Hartmut Holzgraefe]
"I stumbled over the same 'chunked' problem not to long ago as a rule of thumb: never use HTTP/1.1 unless you really know that you have to, claiming to be a HTTP/1.0 client is no problem."

I have posted this as it was something I found very difficult to debug as there is actually nothing wrong with the script. This sort of problem often requires an in depth knowledge of an area that most developers would not have or consider. I would doubt that many, in any, who are reading this have ever read the HTTP RFC 2616 (I doubt also that it is a rivetting read :))  I hope this helps any future developers who are considering the use of high level socket connections with HTTP/1.1.

<dns_get_recordgethostbyaddr>
 Last updated: Fri, 22 Jun 2007
view source | feedback | send page | sitemap | aboutus   
Copyright ® 2002-2003 PHPE.NET. All rights reserved
Last updated:2002-11-22