问题
I have the following code which runs fine on my computer but fails online:
<?PHP
header("Content-Type: text/plain");
$username = "username";
$password = "password";
$url = "http://s6.voscast.com:7158/admin.cgi?mode=viewxml";
$context = stream_context_create(array(
'http' => array (
'method' => "GET",
'header' => "Authorization: Basic ".base64_encode($username.":".$password)."\r\n".
"User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.1 (KHTML, like Gecko) Ubuntu/11.04 Chromium/14.0.835.202 Chrome/14.0.835.202 Safari/535.1"
)
));
print file_get_contents($url, false, $context);
?>
This totally works locally; I'm able to GET the URL behind HTTP-Basic authentication and print it.
However, when I upload it to my server, it doesn't work and spits this out:
<br />
<b>Warning</b>: file_get_contents(http://s6.voscast.com:7158/admin.cgi?mode=viewxml) [<a href='function.file-get-contents'>function.file-get-contents</a>]: failed to open stream: Connection refused in <b>/home/livshin/public_html/wp-content/uploads/_radio/songinfo.php</b> on line <b>16</b><br />
Is there a way I can get a stack trace on this or something which will at least give me better information on what went wrong?
* I've already tested to make sure that something like file_get_contents("http://google.com/") works on the server.
回答1:
Your server probably doesn't allow outgoing connections on port 7158. Google, being on port 80, works correctly. You might need to change the firewall.
来源:https://stackoverflow.com/questions/7960722/file-get-contents-connection-refused