Example 1 — Open a Socket and Send a Tiny HTTP Request
Connect to port 80, write a simple request, read one status line, then close. Output is a typical sample response line.
<?php
$fp = fsockopen("example.com", 80, $errno, $errstr, 10);
if (!$fp) {
echo "Connect failed: $errno $errstr", PHP_EOL;
} else {
$out = "GET / HTTP/1.1\r\n";
$out .= "Host: example.com\r\n";
$out .= "Connection: Close\r\n\r\n";
fwrite($fp, $out);
echo fgets($fp);
fclose($fp);
} How It Works
- fsockopen() tries to connect with a 10 second timeout.
- On failure we print $errno and $errstr.
- On success we write a minimal HTTP request with fwrite().
- fgets() reads the first response line, then fclose() closes the stream.

