php 我自己的网站的 file_get_contents() 连接被拒绝

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3283995/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 09:14:10  来源:igfitidea点击:

file_get_contents() connection refused for my own site

phpcurl

提问by CJD

I've been trying to connect to my own site using both CURL and the PHP file_get_contents() function to get source of my webpage without success. I am running the PHP script on the same server that I am trying to get the HTML source from. CURL doesn't return any errors, not even when using curl_error(), and the PHP file_get_contents() function returns the following:

我一直在尝试使用 CURL 和 PHP file_get_contents() 函数连接到我自己的站点,以获取我的网页源,但没有成功。我正在尝试从中获取 HTML 源代码的同一台服务器上运行 PHP 脚本。CURL 不返回任何错误,即使在使用 curl_error() 时也不返回,并且 PHP file_get_contents() 函数返回以下内容:

Warning: file_get_contents([sitename]) [function.file-get-contents]: failed to open stream: Connection refused in [file path] on line 19.

警告:file_get_contents([sitename]) [function.file-get-contents]:无法打开流:第 19 行的 [文件路径] 中的连接被拒绝。

I've no idea why this is. Why would a server actively refuse this connection? How can I stop it?

我不知道这是为什么。为什么服务器会主动拒绝这个连接?我怎样才能阻止它?

Thanks

谢谢

EDIT:

编辑:

For reference here is my (cURL) code:

参考这里是我的(卷曲)代码:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://www.mydomain.co.uk');
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_REFERER, '');
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-GB; rv:1.9.1.2) Gecko/20090729 Firefox/3.5.2');
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Host: www.mydomain.co.uk')); 

$rawHTML = curl_exec($ch);
$err = curl_error($ch);
curl_close($ch);

print $err;
print 'HTML: ' . $rawHTML;

回答by mvds

Have a look at your firewall settings, they may be a little too strict. What happens if you log in and

看看你的防火墙设置,它们可能有点太严格了。如果您登录并

telnet localhost 80

or the equivalent for your os of choice? And try the same not with localhost but the full ip of your server. Only if it succeeds, you have a curl/php problem.

或您选择的操作系统的等价物?并尝试使用相同的方法,不要使用 localhost,而是使用服务器的完整 ip。只有成功了,你才会遇到 curl/php 问题。

edit:ok, so connection to localhostworks, using file_get_contents("http://localhost/");.

编辑:好的,所以连接到localhost工作,使用file_get_contents("http://localhost/");.

This means that you can access you site through localhost, but you need to override the Host:field sent with the request. This is not exactly normal usage of cURL, but you may try:

这意味着您可以通过 localhost 访问您的站点,但您需要覆盖Host:随请求发送的字段。这不是 cURL 的正常用法,但您可以尝试:

curl_setopt(CURLOPT_HTTPHEADER,array('Host: yourdomain.com'));

while requesting URL http://127.0.0.1/. I wonder if this will be understood by curl but you can give it a shot.

在请求 URL 时http://127.0.0.1/。我想知道 curl 是否会理解这一点,但您可以试一试。

edit^2:If this does not work to trick cURL, just open your own socket connection and make your own request:

编辑^2:如果这对欺骗 cURL 不起作用,只需打开您自己的套接字连接并提出您自己的请求:

$ip = '127.0.0.1';
$fp = fsockopen($ip, 80, $errno, $errstr, 5);
$result = '';
if (!$fp) {
    echo "$errstr ($errno)<br />\n";
} else {
    $out = "GET / HTTP/1.1\r\n";
    $out .= "Host: www.exampl.com\r\n";
    $out .= "Connection: Close\r\n\r\n";
    fwrite($fp, $out);
    while (!feof($fp)) {
        $result .= fgets($fp, 128);
    }
    fclose($fp);
}

(this is an adaption from a php.net example)

(这是对 php.net 示例的改编)

回答by jishi

Most possibly because your site is pointed to a public ip, which in turn is mapped to an internal IP like mvds pointed out.

很可能是因为您的站点指向一个公共 ip,而该公共 ip 又映射到一个内部 IP,如 mvds 指出的那样。

www.domain.com = 234.234.234.234

server ip : 10.0.0.1

www.domain.com = 234.234.234.234

服务器IP:10.0.0.1

firewall maps 234.234.234.234 -> 10.0.0.1from outside networks, but not from internal traffic.

防火墙映射234.234.234.234 -> 10.0.0.1来自外部网络,但不是来自内部流量。

Hence, you need to connect to your machine with the local IP, or localhost (127.0.0.1) but still maintaining the host-header (www.domain.com).

因此,您需要使用本地 IP 或 localhost ( 127.0.0.1)连接到您的机器,但仍需要维护主机头 ( www.domain.com)。

Your options are:

您的选择是:

  1. Make your provider setup correct routing for their external IPs in their firewall. It's doable but often missed since it's rarely needed. One argument for this to work is because you cannot access other sites that resides on the same network segment as you today.

  2. Tell your provider to add www.domain.com -> 127.0.0.1in the hosts-file on your server

  3. Use your own socket-code to "fake" the host-header while still connecting to localhost. There are plenty of example classes for this in PHP and mvds gave you an example already.

  4. Find another way to fetch the information. It's on the same server, isn't it? Fetching it by http seems redundant...

  1. 让您的提供商为其防火墙中的外部 IP 设置正确的路由。这是可行的,但经常被遗漏,因为它很少需要。对此的一个论据是因为您无法访问与您今天位于同一网段的其他站点。

  2. 告诉您的提供商127.0.0.1在您的服务器上的主机文件中添加 www.domain.com ->

  3. 使用您自己的套接字代码“伪造”主机头,同时仍然连接到本地主机。在 PHP 中有很多用于此的示例类,mvds 已经为您提供了一个示例。

  4. 寻找另一种获取信息的方法。它在同一台服务器上,不是吗?通过 http 获取它似乎是多余的......

回答by Jan ?afránek

The whole problem was to add this line to hosts file ( /etc/hosts )

整个问题是将此行添加到主机文件( /etc/hosts )

127.0.0.1 example.com www.example.com

Thanks to jishi! :)

感谢吉时!:)

回答by Misunderstood

The easiest way to retrieve a local file is the use the local path.

检索本地文件的最简单方法是使用本地路径。

file_get_contents('/home/usr/public_html/path/page.html');

The file name path will change from one server to another,but you should get the idea.

文件名路径将从一台服务器更改为另一台服务器,但您应该明白这一点。

In my path the "/usr/" is the user name for the account.

在我的路径中,“/usr/”是帐户的用户名。

Or if you script is in the same directory of the page, you can use:

或者,如果您的脚本位于页面的同一目录中,则可以使用:

file_get_contents('page.html');

回答by Joel Kennedy

I have had this problem before when using a shared server with a hosting company, as many features are turned off. Are you on a shared server/do you get your hosting for free?

我之前在托管公司使用共享服务器时遇到过这个问题,因为许多功能都被关闭了。您是否在共享服务器上/是否免费获得托管?

Check if cURL is enabled on your server too in your PHP config.

检查您的服务器上是否也在 PHP 配置中启用了 cURL。

<?php

// Show all information
phpinfo();

?>

If the data you are trying to access is on the same server, you could use fopen:

如果您尝试访问的数据在同一台服务器上,您可以使用 fopen:

$myFile = "testFile.txt";
$fh = fopen($myFile, 'r');
$theData = fread($fh, 5);
fclose($fh);
echo $theData;

http://php.net/manual/en/function.fopen.php

http://php.net/manual/en/function.fopen.php

回答by Sarfraz

Make sure that you have allow_url_fopenturned on from your php.ini.

确保您allow_url_fopen已从 php.ini 开启。

回答by Richeh

Possibly a stupid question, but you don't have an .htaccess password set do you? If your browser's storing it you might forget, but the PHP server doesn't have it but will still need it to access the site. Almost certainly not, but maybe worth suggesting.

可能是一个愚蠢的问题,但您没有设置 .htaccess 密码,对吗?如果您的浏览器存储了它,您可能会忘记,但 PHP 服务器没有它,但仍需要它来访问站点。几乎可以肯定不是,但也许值得建议。