当前位置:
首页 > 技术站长 > 用Curl获取网页内容

用Curl获取网页内容

[php]< ?php // Plug-in 72: Curl Get Contents
/*
* 插件说明:
* 设计插件的目的是当需要读取网页内容时,可以用本插件取代file_get_contents()函数。
* 它接受网页的URL地址和准备模仿的浏览器用户代理字符串。若调用成功,
* 返回这个网页的内容,若调用失败,返回FALSE。它需要以下参数:
* $url 网页的URL地址
* $agent 浏览器的用户代理字符串。
*/
// This is an executable example with additional code supplied
// To obtain just the plug-ins please click on the Download link

$agent = 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-GB; ' .
'rv:1.9.1) Gecko/20090624 Firefox/3.5 (.NET CLR ' .
'3.5.30729)';
$url = 'http://pluginphp.com';

echo PIPHP_CurlGetContents($url, $agent);

function PIPHP_CurlGetContents($url, $agent)
{
// Plug-in 72: Curl Get Contents
//
// This plug-in fetches a page that may otherwise be
// forbidden using the file_get_contents() function.
// It requires the following arguments:
//
// $url: The URL of the page to fetch
// $agent: A typical browser User Agent string

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_ENCODING, "gzip");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_FAILONERROR, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 8);
curl_setopt($ch, CURLOPT_TIMEOUT, 8);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}

?>
[/php]
(来源:PHP快速开发工具箱 第72个插件,
http://blog.csdn.net/manbujingxin/article/details/7001988)

用Curl获取网页内容:等您坐沙发呢!

发表评论

您必须 [ 登录 ] 才能发表留言!