#!/usr/bin/env php [ 'verify_peer' => false, 'verify_peer_name' => false ] ]); // Connect to server $sock = @stream_socket_client('tcp://' . $host . ':' . $port, $error_code, $error_message, 0.5, STREAM_CLIENT_CONNECT, $context); if ($secure && !@stream_socket_enable_crypto($sock, true, STREAM_CRYPTO_METHOD_TLS_CLIENT)) { throw new Exception('Unable to turn encryption on'); } if ($sock === false) { throw new Exception($error_message, $error_code); } @socket_set_blocking($sock, false); // Read request from file $request = ''; info("REQUEST:\n"); foreach (file($file) as $i => $data) { $CRLFLine = str_replace("\n", "\r\n", str_replace("\r", "", $data)); info(++$i . ': '); echo $data; $request .= $CRLFLine; } // Send request if (@fwrite($sock, $request) !== strlen($request)) { throw new Exception('request not sent'); } @fflush($sock); info("\n--------------------------------------------------------------------------------\n"); info("RESPONSE:\n"); // Non-blocking reading loop $connected = true; do { $read = [$sock]; $write = null; $except = null; $modified = @stream_select($read, $write, $except, 1); if ($modified === false) { $connected = false; info("connection error\n"); } if (stream_get_meta_data($sock)['eof'] ?? true) { $connected = false; } if ($modified === 1) { $data = @fread($sock, 1024); if ($data !== false) { echo $data; } else { $connected = false; } } else { info("waiting...\n"); } } while ($connected); // Close connection @fclose($sock); info("connection closed\n"); } catch (Exception $exception) { fprintf(STDERR, "USAGE:\n" . basename($argv[0]) . " --host|-h host --port|-p port [--secure|-s] file\n"); fprintf(STDERR, "\033[31mERROR: {$exception->getMessage()}\033[0m\n"); }