【问题标题】:file_get_contents works on every site except minefile_get_contents 适用于除我之外的每个站点
【发布时间】:2015-06-12 05:18:27
【问题描述】:

我的代码:

<?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

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

PS:我是否使用“http”或“www”都没有关系。

PSPS:allow_url_fopen 开启

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

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

这会是个问题吗?

【问题讨论】:

  • 您是否有 GUI,如果有,您是否尝试过在 Web 浏览器中访问 URL?如果您有 CLI 访问权限并拥有 Wget,您可以尝试使用 Wget 执行相同的请求并告诉我结果。
  • 连接超时通常意味着防火墙丢弃了请求。服务器操作系统是什么,你能telnet从外部的机器上连接到这台机器的80端口吗?
  • 通过在标题stackoverflow.com/questions/10524748/… 中添加代理来尝试此操作
  • 如果你的意思是像"User-Agent: ".$_SERVER["HTTP_USER_AGENT"]."\r\n" 这样也不会起作用。

标签: php stream file-get-contents


【解决方案1】:

你试过使用 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);

【讨论】:

  • 是的。也尝试使用 cUrl。您的代码可以加载许多网站,但在我的情况下它只是空白。 (即使使用 print_r)
【解决方案2】:

尝试将 max execution time 设置为较大的 period ,它可能已设置为较低。 ini_set('max_execution_time', 60); //1 分钟

【讨论】:

    【解决方案3】:

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

    【讨论】:

      【解决方案4】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-03-19
        • 1970-01-01
        • 1970-01-01
        • 2013-03-20
        • 2016-03-27
        • 2022-08-21
        • 1970-01-01
        相关资源
        最近更新 更多