首页 > 开发 > PHP > 正文

php file_get_contents返回空 无效解决办法

2024-05-04 21:59:18
字体:
来源:转载
供稿:网友

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时我们利用如下代码:

  1. <?php 
  2. $file_contents = file_get_contents(''http://www.Vevb.com/''); 
  3. echo $file_contents
  4. ?> 

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

  1. function file_get_content($url) { 
  2. if (function_exists(‘file_get_contents')) { 
  3. $file_contents = @file_get_contents($url); 
  4. if ($file_contents == ”) { 
  5. $ch = curl_init(); 
  6. $timeout = 30; 
  7. curl_setopt($ch, CURLOPT_URL, $url); 
  8. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
  9. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout); 
  10. $file_contents = curl_exec($ch); 
  11. curl_close($ch); 
  12. return $file_contents
  13. }

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表