techsir 登陆 |注册|TS首页
  首页 快活林 猿氏悟语

file_get_contents和curl函数用法

By: 冰客 发表于 2009-2-19 10:53:37 · 71978次点击   回复:0   
file_get_contents ()应用很简单,但是有的服务器php.ini设置如果关闭allow_url_fopen,这个函数就失效了,一般个人服务器可以设置,但是如果是虚拟主机就不在自己掌控范围内了。但是curl 是另外一个打开远程页面的内容的函数用法如下:

<?php
// 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;
71978次点击
0个回复  |  直到 2009-2-19 10:53:37
添加一条新回复
您需要登录后才可以回帖 登录 | 成为会员 新浪微博登陆

标签云|手机版|科技先生 ( 京ICP备07036130号 Powered by Discuz! X )

GMT+8, 2024-5-19 04:54