Heim  >  Artikel  >  Backend-Entwicklung  >  file_get_contents和curl函数用法_PHP教程

file_get_contents和curl函数用法_PHP教程

WBOY
WBOYOriginal
2016-07-13 17:37:35933Durchsuche

file_get_contents ()应用很简单,但是有的服务器php.ini设置如果关闭allow_url_fopen,这个函数就失效了,一般个人服务器可以设置,但是如果是虚拟主机就不在自己掌控范围内了。但是curl 是另外一个打开远程页面的内容的函数用法如下:

// create a new curl resource
$ch = curl_init();

// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, "http://www.example.com/");
curl_setopt($ch, CURLOPT_HEADER, 0);

// grab URL and pass it to the browser
curl_exec($ch);

// close curl resource, and free up system resources
curl_close($ch);
?>

当然此功能也有被关闭的可能。

使用以上2个方法可以使用function_exists()判断使用

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;
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/486557.htmlTechArticlefile_get_contents ()应用很简单,但是有的服务器php.ini设置如果关闭allow_url_fopen,这个函数就失效了,一般个人服务器可以设置,但是如果是...
Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn