PHP Programmers Section

Using fsockopen in PHP

by Dixanta Shrestha
Dixanta Shrestha Advanced  
fsockopen

(PHP 4, PHP 5)

fsockopen — Open Internet or Unix domain socket connection
Descriptionresource fsockopen ( string $hostname [, int $port [, int &$errno [, string &$errstr [, float $timeout]]]] )


Initiates a socket connection to the resource specified by
hostname.


PHP supports targets in the Internet and Unix domains as described in
Appendix Q, List of Supported Socket Transports. A list of supported transports can also be
retrieved using stream_get_transports().


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

 
hostname

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.
port

The port number.
errno

If provided, holds the system level error number 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.
errstr

The error message as a string.
timeout

The connection timeout, in seconds.


Note: 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.

Return Values

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 FALSEChangeLog

 




Version
Description




4.3.0
Added support for the timeout parameter on
win32.



4.3.0
SSL and TLS over TCP/IP support was added.



4.0.0
UDP support was added.



3.0.9
The timeout parameter was added.




Examples


<?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);
}
?>


<?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);
}
?>

easy get UIDL.
A code is better than the feof.

<?php

$fp = fsockopen($host, 110, $errno, $errstr, 5);
if ($fp > 0) {
    fread($fp,1024);
    fputs($fp, "USER $user\r\n");
    fread($fp,1024);
    fputs($fp, "PASS $pass\r\n");
    fread($fp,1024);
    fputs($fp, "UIDL\r\n");
    fgets($fp,1024);
    do {
        $line = rtrim(fgets($fp, 1024));
        if ($line == '.') break;
        list(, $UIDL) = split(' ', $line, 2);
        echo $UIDL;
    } while ($line != '.');
   
    fputs($fp, "QUIT\r\n");
}
fclose($fp);

?>

To check if a Url is Online (with http and https)
Using @fgets to hide Warning when using SSL
(Bug: "Warning: function.fgets SSL: fatal protocol error", http://bugs.php.net/bug.php?id=23220)

<?php

    function isOnline($url) {
        if (!$url_info = parse_url($url)) {
            return false;   
        }
       
        switch ($url_info['scheme']) {
            case 'https':
                $scheme = 'ssl://';
                $port = 443;
                break;
            case 'http':
            default:
                $scheme = '';
                $port = 80;   
        }
       
        $data = "";
        $fid = @fsockopen($scheme . $url_info['host'], $port, $errno, $errstr, 30);
        if ($fid) {
            fputs($fid, 'HEAD ' . (isset($url_info['path'])? $url_info['path']: '/') . (isset($url_info['query'])? '?' . $url_info['query']: '') . " HTTP/1.0\r\n" .
                        "Connection: close\r\n" .
                        'Host: ' . $url_info['host'] . "\r\n\r\n");   
            while (!feof($fid)) {
                $data .= @fgets($fid, 128);
            }
            fclose($fid);
            return !empty($data);
        } else {
            return false;
        }
    }

?>
Nov 21st 2007 22:34

Sponsor Ads


Comments

No comment, be the first to comment.
You are not yet a member of this group.