title: PHP Socket GET Request
body: <p>I am using the following script to send a GET request to a site however, I keep getting the error, No response from the Server.</p>
  
  <ol>
  <li>I have run wireshark to see any traffic being generated as a result of running this script, but there is no traffic.</li>
  <li>I can connect to the site from the Browser and I can also retrieve the contents by using the file_get_contents function, however using sockets, I am unable to do it.</li>
  </ol>
  
  <p>Here is the code which makes use of sockets to send the request to the site and it returns the error: "No response from the Server" if the connection was not successful.</p>
  
  <pre><code>&lt;?php
  
  error_reporting(0);
  set_time_limit(0);
  ini_set("default_socket_timeout", 5);
  
  function http_send($host, $packet)
  {
      if (!($sock = fsockopen($host, 80)))
          die( "\n[-] No response from {$host}:80\n");
  
      fwrite($sock, $packet);
      return stream_get_contents($sock);
  }
  
  $host="http://example.com";
  $path="/";
  $packet  = "GET {$path} HTTP/1.0\r\n";
  $packet .= "Host: {$host}\r\n";
  $packet .= "Connection: close\r\n\r\n";
  
  http_send($host, $packet);
  
  ?&gt;
  </code></pre>
  
  <p>So, what is wrong in the code above that it is unable to send the GET request?</p>
  
  <p>It appears ok to me, it will form the HTTP Request Headers for the GET request and store in the $packet variable.</p>
  
  <p>$host is the destination site name.</p>
  
  <p>Then it calls the function, http_send which will send the request.</p>
  
  <p>Error Output: [-] No response from <a href="http://example.com:80" rel="nofollow">http://example.com:80</a></p>