问题
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?
回答1:
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!
回答2:
This means that your script cannot resolve the hostname to an IP address. Probably there is a problem with your dns configuration.
回答3:
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.
来源:https://stackoverflow.com/questions/21075434/fsockopen-returning-server-error0