title: How to connect telnet and send command and write output into text file using php
body: <p>i need to telnet to a port and send command and write the output into a txt file using PHP.How i do it?</p>
  
  <p>in this forum have a same question name <a href="https://stackoverflow.com/questions/905348/telnet-connection-using-php">telnet connection using PHP</a> but their have a solution link and the solution link is not open so i have to make the question again.</p>
  
  <p>Also i try the code below from <a href="http://php.net/manual/en/function.fsockopen.php" rel="nofollow noreferrer">php site</a> but it does not save the proper output into a text file.Code:</p>
  
  <pre><code>&lt;?php
  $fp = fsockopen("localhost", 80, $errno, $errstr, 30);
  if (!$fp) {
      echo "$errstr ($errno)&lt;br /&gt;\n";
  } else {
      $out = "GET / HTTP/1.1\r\n";
      $out .= "Host: localhost\r\n";
      $out .= "Connection: Close\r\n\r\n";
      fwrite($fp, $out);
      while (!feof($fp)) {
          echo fgets($fp, 128);
      }
      fclose($fp);
  }
  ?&gt;
  </code></pre>
  
  <p>So,please help me to solve the problem.<strong>How i telnet to localhost port 80 and send command GET / HTTP/1.1 and write the output into a text file?</strong> </p>