php中封装的curl函数(抓取数据)

介绍一个封闭好的函数,封闭了curl函数的常用步骤,方便抓取数据。

代码如下:

<?php
/**
* 封闭好的 curl函数
* 用途:抓取数据
* edit by www.jbxue.com
*/
function curl($url, $ifpost = 0, $datafields = '', $cookiefile = '', $v = false) { 
$header = array("Connection: Keep-Alive","Accept: text/html, application/xhtml+xml, */*", "Pragma: no-cache", "Accept-Language: zh-Hans-CN,zh-Hans;q=0.8,en-US;q=0.5,en;q=0.3","User-Agent: Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; WOW64; Trident/6.0)"); 
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, $url); 
curl_setopt($ch, CURLOPT_HEADER, $v); 
curl_setopt($ch, CURLOPT_HTTPHEADER, $header); 
$ifpost && curl_setopt($ch, CURLOPT_POST, $ifpost); 
$ifpost && curl_setopt($ch, CURLOPT_POSTFIELDS, $datafields); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); 
curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate'); 
$cookiefile && curl_setopt($ch, CURLOPT_COOKIEFILE, $cookiefile); 
$cookiefile && curl_setopt($ch, CURLOPT_COOKIEJAR, $cookiefile); 
$r = curl_exec($ch); 
curl_close($ch); 
return $r; 
}?>
posted @ 2013-07-23 06:32  一觉睡到天黑黑  阅读(295)  评论(0编辑  收藏  举报