>  기사  >  php教程  >  php file_get_contents返回空 无效解决办法

php file_get_contents返回空 无效解决办法

WBOY
WBOY원래의
2016-05-24 13:00:492780검색

file_get_contents函数多用来于来采集远程服务器上的内容,但使用file_get_contents函数之前我们在php.ini中是必须把allow_url_fopen开启才行

问题描述:

fopen(),file_get_contents(),getimagesize() 等都不能正常获得网络上的内容,具体表现为凡参数是URL的,一律返回空值.

如果是windows可找开 allow_url_fopen开启,如果是否linux中可以重新编译PHP,去掉-with-curlwrapper 参数——编译前记得先执行 make clean.

windows 在未开启allow_url_fopen时我们利用如下代码:

<?php 
$file_contents = file_get_contents("http://www.phprm.com/"); 
echo $file_contents;

 

是获取不到值的,但我们可以利用function_exists来判断此函数是否可用,代码如下:

<?php
function file_get_content($url) {
    if (function_exists(&#39;file_get_contents&#39;)) {
        $file_contents = @file_get_contents($url);
    }
    if ($file_contents == &#39;&#39;) {
        $ch = curl_init();
        $timeout = 30;
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
        $file_contents = curl_exec($ch);
        curl_close($ch);
    }
    return $file_contents;
}


本文地址:

转载随意,但请附上文章地址:-)

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.