file_get_contents()连接拒绝我自己的网站 [英] file_get_contents() connection refused for my own site

查看:145
本文介绍了file_get_contents()连接拒绝我自己的网站的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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


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


我不知道为什么这是。为什么服务器会主动拒绝此连接?如何停止?



感谢



编辑:


$ b b

这里是我的(cURL)代码:

  $ 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;


解决方案

查看您的防火墙设置,有点太严格了。如果您登录,会出现什么情况,

  telnet localhost 80 

或您的选择的等效物?并尝试相同不是与localhost,而是您的服务器的完整的ip。只要成功,就会出现curl / php问题。



编辑:确定,所以连接到 localhost 使用 file_get_contents(http:// localhost /);



<这意味着您可以通过localhost访问您的网站,但您需要覆盖与请求一起发送的 Host:字段。这不是正常使用cURL,但你可以尝试:

  curl_setopt(CURLOPT_HTTPHEADER,array('Host:yourdomain.com '));请求URL  http://127.0.0.1/   

$ c>。我不知道这是不是会被卷曲理解,但你可以给它一个镜头。



编辑^ 2: cURL,只需打开自己的套接字连接,并自己请求:

  $ ip ='127.0.0.1'; 
$ fp = fsockopen($ ip,80,$ errno,$ errstr,5);
$ result ='';
if(!$ fp){
echo$ errstr($ errno)< br /> \\\
;
} else {
$ out =GET / HTTP / 1.1\r\\\
;
$ out。=主机:www.exampl.com\r\\\
;
$ out。=连接:关闭\r\\\
\r\\\
;
fwrite($ fp,$ out);
while(!feof($ fp)){
$ result。= fgets($ fp,128);
}
fclose($ fp);
}

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


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:

Warning: file_get_contents([sitename]) [function.file-get-contents]: failed to open stream: Connection refused in [file path] on line 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;

解决方案

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.

edit: ok, so connection to localhost works, using 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:

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.

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

$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)

这篇关于file_get_contents()连接拒绝我自己的网站的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆