How does the fwrite function interact with the socket returned by fsockopen in PHP?

The fwrite function in PHP is used to write data to a file or resource. When using fwrite with a socket returned by fsockopen, you can write data to the socket to send it to a remote server. To do this, you need to open a socket connection using fsockopen, then use fwrite to send data over the socket.

$socket = fsockopen('example.com', 80, $errno, $errstr, 30);
if (!$socket) {
    echo "Error: $errstr ($errno)\n";
} else {
    fwrite($socket, "GET / HTTP/1.1\r\nHost: example.com\r\n\r\n");
    
    while (!feof($socket)) {
        echo fgets($socket, 1024);
    }
    
    fclose($socket);
}