TechniqueTo connect to a remote machine by using a TCP connection, use PHP's fsockopen() function: <?php $fp = fsockopen("www.zend.com", 80, $errno, $errstr, 30); if (!$fp){ die($errstr); } fputs($fp, "HEAD /HTTP/1.0 \r\n\r\n"); while (!feof($fp)) { $line = fgets($fp, 2048); echo $line; } fclose($fp); ?> Or, for more control, use PHP's sockets module (you must compile with --enable-sockets for this to work): <?php $url = 'www.zend.com'; $serv_port = getservbyname('www', 'tcp'); $address = gethostbyname($url); $sock = socket(AF_INET, SOCK_STREAM, 0); if ($sock < 0) { die(strerror($sock)); } $res = connect($sock, $address, $serv_port); if ($res < 0) { die(strerror($res)); } $data = "HEAD / HTTP/1.0\r\n\r\n"; write($sock, $data, strlen($data)); while (read($sock, $response, 2048)) { print $response; } close($sock); ?> CommentsThe two programs in the solution do the same thing, but the first one uses the standard fsockopen() function, and the second uses the sockets module distributed with PHP. The fsockopen() function provides a generic, simple interface to connecting to a socket. However, if you want more control, you can use PHP's sockets module, which gives you more direct access to the lower-level C APIs. |