The easiest method for PHP chat is via HTTP (port 80). However, if you are required to use sockets you would want to use the PHP fsockopen() module (php.net/fsockopen). This module will allow you to open a socket connection for both reading/writing. It also allows you to manage the resource connection as if you where working with a file (thank you PHP). Meaning you can use the PHP file modules to work with the socket connection, fread(), fwrite(), etc...
resource fsockopen ( string $hostname [, int $port [, int &$errno [, string &$errstr [, float $timeout ]]]] )
Example #1 fsockopen() Example (php.net/fsockopen)
PHP Code:
<?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);
}
?>
Bookmarks