我用file_get_contents("$url");打不开服务器上本地项目中的网页
后来我试试外网,发现file_get_contents("www.baidu.com"),这样可以很奇怪了,不知道为什么本地的不行,外网的可以,本地的报错:Warning: file_get_contents(http://localhost:8070/index.php/%E4%BD%BF%E7%94%A8%E5%B8%AE%E5%8A%A9) [function.file-get-contents]: failed to open stream: 由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败。 in C:\wamp\www\test.php on line 3
是不是apache的配置问题,还是什么的?禁止打开吗?项目是可以浏览的,file_get_contents打不开。

解决方案 »

  1.   

    这是什么网址 http://localhost:8070/index.php/使用帮助
      

  2.   

    1、测试一下http://localhost:8070/index.php/%E4%BD%BF%E7%94%A8%E5%B8%AE%E5%8A%A9能否直接打开
    2、将localhost 修改为127.0.0.1试试
    3、file_get_contents("www.baidu.com")最修改为file_get_contents("http://www.baidu.com")
    另外建议
    在抓取网络内容时建议不要使用file_get_contents,因为它效率比较低。
    使用curl实现更高效一些
    实现方法:/**
     * Send a GET requst using cURL
     * @param string $url to request
     * @param array $get values to send
     * @param array $options for cURL
     * @return string
     */
    function curl_get($url, array $get = NULL, array $options = array()) {
        $defaults = array(
            CURLOPT_URL => $url . (strpos($url, '?') === FALSE ? '?' : '') . http_build_query($get),
            CURLOPT_HEADER => 0,
            CURLOPT_RETURNTRANSFER => TRUE,
            CURLOPT_TIMEOUT => 4
        );
        $ch = curl_init();
        curl_setopt_array($ch, ($options + $defaults));
        if (!$result = curl_exec($ch)) {
            trigger_error(curl_error($ch));
        }
        curl_close($ch);
        return $result;
    }
    $get = array('wd'=>'php');
    $html = curl_get("http://www.baidu.com/s?",$get);
    echo "<textarea cols='80' rows='20'  >{$html}</textarea>";
      

  3.   

    使用curl需要开启php_curl扩展。
      

  4.   

    我擦,你有%E4%BD%BF%E7%94%A8%E5%B8%AE%E5%8A%A9这个页面吗。。
      

  5.   


    哦哦,是urlencode的。你看看httpd.conf,你的服务端bind在什么IP上。
      

  6.   

    有那个页面,php.ini里allow_url_fopen =on
    http://localhost:8070/index.php/%E4%BD%BF%E7%94%A8%E5%B8%AE%E5%8A%A9能直接打开
    就是file_get_contents().这里由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败..
    不用localhost,用域名也可以打开.
    www.baidu.com是我简写了,外网是可以打开的,就是本地的不行,应该是wamp的配置问题吧?
      

  7.   

    现在测试就在服务器本机上测试代码,应该是wamp配置的问题吧?
      

  8.   

    你尝试一下抓取其他的本机页面看行不行。 allow_url_fopen =On  打开了吗?
      

  9.   

    项目里的都不行,可以访问数据库,就是到了file_Get_contents这里就不行,php文件夹里的php.ini中的allow_url_fopen = on 打开了这个是wamp里的环境,PHPnow上的项目可以抓取。
      

  10.   

    echo ini_get('allow_url_fopen');  //看是否为1 
      

  11.   

    打印结果是1
    echo ini_get('allow_url_fopen'); 
      

  12.   

    把localhost:8070改成127.0.0.1:8070就可以了,host文件的问题吗?
      

  13.   

    我没权限在windows - system32 - drivers - etc - hosts 里改动
      

  14.   

    可能没有设置hosts 127.0.0.1 到 localhost的解析。既然你没权限的话,那就乖乖的用ip吧。
      

  15.   

    除了file_get_contents()里不能用localhost之外,其他用localhost都可以。应该只要改file_get_contents()里的链接吧