php专区

 首页 > php专区 > PHP应用 > php函数大全 > file_get_contents被屏蔽解决方法 - php函数

file_get_contents被屏蔽解决方法 - php函数

分享到:
【字体:
导读:
          在php中file_get_contents函数可直接采集远程服务器内容,然后保存到一个变量中了,介理一般都会把file_get_contents、fsockopen等一些IO操...

file_get_contents被屏蔽解决方法

在php中file_get_contents函数可直接采集远程服务器内容,然后保存到一个变量中了,介理一般都会把file_get_contents、fsockopen等一些IO操作的函数禁用掉,因为它们怕被 DDOS.

那么一般情况下,我们改不了服务器的 inc.php,只能自己写一套IO来代替上面的PHP函数了,代码如下:

$url = file_get_contents('http://www.phpfensi.com/');

我们可以用下面的代码代替:

  1. //禁用file_get_contents的解决办法  
  2. $ch = curl_init();  
  3. $timeout = 10; // set to zero for no timeout  
  4. curl_setopt ($ch, CURLOPT_URL,'http://www.hzhuti.com/');  
  5. curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);   
  6. curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);  
  7. $url = curl_exec($ch); 

curl是一个利用URL语法规定来传输文件和数据的工具,支持很多协议,如HTTP、FTP、TELNET等,它不会被服务器禁用,所以我们可以用来模拟file_get_contents一样打开一条URL.

利用function_exists函数来判断php是否支持一个函数可以轻松写出下面函数

  1.  function vita_get_url_content($url) {  
  2.  if(function_exists('file_get_contents')) {  
  3.  $file_contents = file_get_contents($url);  
  4.  } else {  
  5.  $ch = curl_init();  
  6.  $timeout = 5;   
  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.  }  
  13.  return $file_contents;  
  14.  }  
  15.  ?> 
分享到:
file_get_contents不能获取带端口的网址 ...
file_get_contents不能获取带端口的网址 先们来了解file_get_contents() 函数,官方介绍说它是把整个文件读入一个字符串中,例子:   输出:This is a test file with test text.同样此函数还可以用于获取远程服务器上的内容:file_get_contents('http://www.phpfensi.com'...
php urlencode()函数实现URL编码解析 - p...
php urlencode()函数实现URL编码解析 urlencode (PHP 4, PHP 5) urlencode — 编码 URL 字符串 说明:string urlencode ( string $str ),此函数便于将字符串编码并将其用于 URL 的请求部分,同时它还便于将变量传递给下一页。 参数:str 要编码的字符串。 返回值:返回字符串,此字符串中...
  •         php迷,一个php技术的分享社区,专属您自己的技术摘抄本、收藏夹。
  • 在这里……