插件72:用Curl获取网页内容

<?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;
}

?>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值