【问题标题】:Cannot load a specific URL with file_get_contents()无法使用 file_get_contents() 加载特定 URL
【发布时间】:2016-02-05 21:46:05
【问题描述】:

我正在刮http://www.beautyinzone.net/

我正在使用简单的file_get_contents() 来加载此 URL。

它在我的本地主机上完美运行。但是当我在我的实时服务器上上传时它不起作用。

Warning: file_get_contents(http://www.beautyinzone.net/): failed to open stream: Connection timed out in script.php on line 36 page

如果我打开其他 URL,它就可以正常工作。

网址

我也尝试过使用 cURL,但它显示空白页面,没有返回任何内容。

我的脑海里闪过一个念头,也许我被屏蔽了,但不应该是那个说网站使用我的服务器的那样。

哪些可能的问题会阻止我加载该 URL?

【问题讨论】:

  • file_get_contents 对我有用
  • 是的,它在 localhost 上也是如此……但是请看……我在我的服务器上放置了刮板……它不会刮掉 coszi.com/crawl/beautyinzone.php
  • 我想不出除了服务器被阻塞之外的任何其他原因。
  • @Barmar 我也想不通...这种奇怪的情况...

标签: php curl


【解决方案1】:

它对我有用。 尝试通过以下代码获取错误

if (!$content = file_get_contents("http://www.beautyinzone.net/")) {
    $error = error_get_last();
    echo $error['message'];
} else {
    echo "working fine";
}

【讨论】:

    【解决方案2】:

    由于远程服务器延迟回复,您再次收到 http 超时错误。这很正常。

    TCP 套接字已设置超时,当达到此超时时会引发超时错误。一般超时时间为 30 秒(默认使用 default_socket_timeout php.ini 设置)。

    也许你想自己设置(增加)一个自定义超时:

    <?php
    
    $http_context = stream_context_create(array(
        'http' => array(
            'timeout' => 60.0 # 60 seconds
        )
    ));
    $url = 'http://www.beautyinzone.net';
    $content = file_get_contents($url, false, $http_context);
    

    通过 cURL 只需使用:

    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 60); 
    

    【讨论】:

    • 也试过了......得到同样的错误......并且没有从 cURL 返回任何东西
    • 嗯,你用其他网址测试过吗?
    • 是的,我已经测试了其他一些 URL,并且加载正常
    • 看起来我被禁止进入那里......即使我从来没有使用我的服务器刮过那个网站:(
    • hmm 设置一个User-agent 标头怎么样?
    【解决方案3】:

    试试这个 cURL sn-p

    function retrieve_curl_info() {
         $ch = curl_init();
         curl_setopt_array($ch, array(
             CURLOPT_URL => 'http://www.beautyinzone.net/',
             CURLOPT_RETURNTRANSFER => true
         ));
         $output = curl_exec($ch);
         curl_close($ch);
         return $output
    }
    

    另外,看看这个thread

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-09-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-19
      • 2011-06-03
      • 2013-06-03
      相关资源
      最近更新 更多