file_get_contents适用于除我之外的所有站点


file_get_contents works on every site except mine

我的代码:

<?php
    $url = "http://www.myurl.com/";
    /*$opts = array(
        'http'=>array(
            'method'=>"GET",
            'header'=>"User-Agent: ".$_SERVER['HTTP_USER_AGENT']
        )
    );
    $context = stream_context_create($opts);*/
    $content = file_get_contents($url);
    echo $content;
?>

我收到以下错误:

Warning: file_get_contents(http://www.myurl.com/) [function.file-get-contents]: failed to open stream: Connection timed out in /home/content/myurl/contents.php on line 10

当我创建或不创建流上下文时,这些代码对我来说都不起作用。有趣的是,它可以在我尝试过的任何其他网站上工作,除了我自己的网站。

附言:不管我是否使用"http"或"www"。

PSPS:allow_url_fopen打开

PSPSP:我在.htaccess:中设置了以下内容

<filesMatch "''.(html|htm|php)$">
     Header set Cache-Control "max-age=1, private, must-revalidate"
     </filesMatch>

这会是个问题吗?

你试过使用curl吗?

    function curl($url){
                $ch = curl_init();
                curl_setopt($ch, CURLOPT_URL, $url);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
                curl_setopt($ch,CURLOPT_FOLLOWLOCATION,true);
                $data = curl_exec($ch);
                curl_close($ch);
                return $data;
      }

      $url = 'http://www.myurl.com/';
      $contents = curl($url);

尝试将最大执行时间设置为较大的周期,可能已设置为较低的周期。ini_set('max_execution_time',60)//1分钟

您的服务器应该有一个防火墙,限制您连接到外部资源。所以检查一下你的防火墙。

它应该在共享主机上工作,但如果它是您自己的服务器,则可能是服务器/LAN上的网络或DNS问题导致服务器无法将"www.myurl.com"解析到自己的地址。如果您不使用虚拟主机(在apache上)或等效机制,则可以使用http://localhost/'而不是