file_get_contents被屏蔽解决方法_PHP教程

WBOY
Libérer: 2016-07-13 10:57:22
original
1378 Les gens l'ont consulté

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

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

 代码如下 复制代码


$url = file_get_contents('http://www.bKjia.c0m/');

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

 代码如下 复制代码

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


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

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

 

 代码如下 复制代码
 function vita_get_url_content($url) {
 if(function_exists('file_get_contents')) {
 $file_contents = file_get_contents($url);
 } else {
 $ch = curl_init();
 $timeout = 5; 
 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;
 }
 ?>

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/632091.htmlTechArticle在php中file_get_contents函数可直接采集远程服务器内容,然后保存到一个变量中了,介理一般都会把file_get_contents、fsockopen等一些IO操作的函数...
Étiquettes associées:
source:php.cn
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal
À propos de nous Clause de non-responsabilité Sitemap
Site Web PHP chinois:Formation PHP en ligne sur le bien-être public,Aidez les apprenants PHP à grandir rapidement!