Search code examples
phpfsockopen

fsockopen returning server error(0)


I'm checking fsockopen in Ubuntu server 13.04 with this code:

<?php 
  $fp = fsockopen("www.google.com", 80, $errno, $errstr, 30); 
  if (!$fp) { 
      echo "$errstr ($errno)<br />\n"; 
  } else { 
      $out = "GET / HTTP/1.1\r\n"; 
      $out .= "Host: www.example.com\r\n"; 
      $out .= "Connection: Close\r\n\r\n"; 

      fwrite($fp, $out); 
      while (!feof($fp)) { 
          echo fgets($fp, 128); 
      } 
      fclose($fp); 
  } 
?>

and the server returning

php_network_getaddresses: getaddrinfo failed: System error (0)

Any help with this?


Solution

  • There is no problem in your code - it's fine and working!

    Most probably the firewall is blocking 80 port and this is why you can't connect.

    Check your connection from the console and see what you get:

    ping google.com
    

    EDIT 1:

    Most likely you have a problem in your /etc/resolv.conf or /etc/hosts. To solve this you could refer to: Ping: Unknown host. If you can't just post output of those files and we'll see of what I could do!