4

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?

Markus Malkusch
  • 7,738
  • 2
  • 38
  • 67
sukorenomw
  • 68
  • 1
  • 7
  • its not about the code, its about the server config maybe, any help with this ? for addition, i can't ping to any host, the server says "unknown host" just like no internet connection from the server – sukorenomw Jan 12 '14 at 14:12

3 Answers3

2

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!

Community
  • 1
  • 1
Ilia Ross
  • 13,086
  • 11
  • 53
  • 88
  • yes, i think its os problem, but im not sure where to config, i try to ping www.google.com the server say, unknown host, but im connected to the server via ssh – sukorenomw Jan 12 '14 at 14:09
  • @user3187396 take a look at `EDIT 1` and let me know if you can fix it. – Ilia Ross Jan 12 '14 at 14:14
1

This means that your script cannot resolve the hostname to an IP address. Probably there is a problem with your dns configuration.

Lorenz Meyer
  • 19,166
  • 22
  • 75
  • 121
0

When I try on my Ubuntu it works:

<HTTP/1.1 302 Found
Location: http://www.google.com/
Cache-Control: private
Content-Type: text/html; charset=UTF-8
X-Content-Type-Options: nosniff
Date: Sun, 12 Jan 2014 13:55:40 GMT
Server: sffe
Content-Length: 219
X-XSS-Protection: 1; mode=block
Alternate-Protocol: 80:quic
Connection: close

<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
<TITLE>302 Moved</TITLE></HEAD><BODY>
<H1>302 Moved</H1>
The document has moved
<A HREF="http://www.google.com/">here</A>.
</BODY></HTML>

The issue might come from your connection rather than your code. Check e.g. if your behind a firewall.

gturri
  • 13,807
  • 9
  • 40
  • 57