php curl

function getHtml($url,$gzip=false,$proxy=false,$ip=null,$getCookie=false,$useCookie=false, $cookList=array(), $post=false, $postData=null)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36");
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT,60);

if($gzip === true)
{
curl_setopt($ch, CURLOPT_ENCODING, 'gzip');
}

if($proxy === true)
{
curl_setopt($ch, CURLOPT_PROXY, $ip);
}

if($getCookie === true)
{
curl_setopt($ch, CURLOPT_HEADER, 1);
}

if($useCookie === true)
{
if(!empty($cookieList)) {
foreach ($cookList as $c) {
curl_setopt($ch, CURLOPT_COOKIE, $c);
}
}
}

if($post === true)
{
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
}

$html = curl_exec($ch);
curl_close($ch);
if( empty($html) ){
return false;
}
$returnData = array("html" => $html);
if($getCookie === true)
{
preg_match_all('/Set-Cookie:(.*);/iU',$html,$cookieList); //正则匹配
if(!empty($cookieList[1]))
{
$returnData['cookie'] = $cookieList[1];
}else{
$returnData['cookie'] = "";
}
}

return $returnData;
}

原文地址:https://www.cnblogs.com/liuwenbohhh/p/5063361.html