话不多说直接上代码:
<?
function curl($url, $paras = []){
$paras = array_change_key_case($paras, CASE_LOWER); //键名全部转小写
$dl=array("82.156.212.19","8.142.171.20","42.193.126.5"); //代理列表
shuffle($dl); if($paras['daili']){ $url='http://'.$dl[0] .'/?url='.urlencode($url);
$paras['post']=$paras; $paras['post']=array_merge($paras['post'],['daili_status'=>1]); unset($paras['post']['daili'],$paras['header']); }
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
if ($paras['header']) { //伪造Header
$Header = $paras['header'];
} else {
$Header[] = "Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9";
$Header[] = "Accept-Encoding:gzip, deflate, br";
$Header[] = "Accept-Language:zh-CN,zh;q=0.9";
$Header[] = "Connection:max-age=0";
}
curl_setopt($ch, CURLOPT_HTTPHEADER, $Header);
if ($paras['ctime']) { // 连接超时
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $paras['ctime']);
} else {
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
}
if ($paras['rtime']) { // 读取超时
curl_setopt($ch, CURLOPT_TIMEOUT, $paras['rtime']);
}
if ($paras['ip']) { //伪造IP
$num=count(explode('.',$paras['ip'])); //获取数组长度
if($num!=4&&$num!=6||!filter_var($paras['ip'], FILTER_VALIDATE_IP)){
curl_setopt($ch, CURLOPT_HTTPHEADER, array("X-FORWARDED-FOR:220.181.".rand(1,230).".".rand(1,230), "CLIENT-IP:220.181.".rand(1,230).".".rand(1,230)));
}else{ curl_setopt($ch, CURLOPT_HTTPHEADER, array("X-FORWARDED-FOR:".$paras['ip'], "CLIENT-IP:".$paras['ip'])); }
}
if(!$paras['loadurl']){ curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); }//默认跟随跳转抓取
if ($paras['post']) { //POST提交
//'post'=>'id=1&key=key'或者'post'=>[ 1=>'内容',],第二种键名必须对上号,有些网址不允许数组方式
if(is_array($paras['post'])){ $paras['post']=http_build_query($paras['post']); } //若为数组,则执行参数拼接,防止有些头部固定这种格式
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $paras['post']);
}
if ($paras['get_header']) { //查看返回Header信息
curl_setopt($ch, CURLOPT_HEADER, true);
}
if($paras['cookie']) { //携带Cookie,必须用;分隔
if(is_array($paras['cookie'])){ foreach($paras['cookie'] as $key=>$value){
if(preg_match("/^[1-9][0-9]*$/",$key)&&strlen($key)<=2){ $cookie.=$value.'; ';
}else{ $cookie.=$key.'='.$value.'; '; } } //有些需要urlencode编码格式
}else{ $cookie=$paras['cookie']; }
curl_setopt($ch, CURLOPT_COOKIE, $cookie);
}
if ($paras['refer']) { //伪造来访网址
if ($paras['refer'] == 1) {
curl_setopt($ch, CURLOPT_REFERER, 'http://m.qzone.com/infocenter?g_f=');
} else {
curl_setopt($ch, CURLOPT_REFERER, $paras['refer']);
}
}
if ($paras['ua']) { //伪造UA
curl_setopt($ch, CURLOPT_USERAGENT, $paras['ua']);
} else {
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Linux; U; Android 11; zh-cn; PEGT00 Build/RKQ1.200903.002) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/83.0.4103.106 MQQBrowser/11.8 Mobile Safari/537.36"); //默认QQ手机浏览器UA值
}
if ($paras['nobody']&&!$paras['daili']) { //不返回网页源代码信息
curl_setopt($ch, CURLOPT_NOBODY, 1);
}
curl_setopt($ch, CURLOPT_ENCODING, "gzip");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
if (($paras['getcookie']||$paras['get_cookie'])&&!$paras['daili']) { //获取请求的全部信息
curl_setopt($ch, CURLOPT_HEADER, 1);
$result = curl_exec($ch);
preg_match_all("/Set-Cookie: (.*?);/m", $result, $matches);
$headerSize = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$header = substr($result, 0, $headerSize);
$body = substr($result, $headerSize);
$ret = [
"Cookie"=>$matches,"cookie"=>$matches, "body" => $body, "header" => $header, 'code' => curl_getinfo($ch, CURLINFO_HTTP_CODE), //code是响应状态码
]; curl_close($ch);
if($paras['daili_status']){ return json_encode($ret); }
return $ret; }
$ret = curl_exec($ch);
if ($paras['loadurl']&&!$paras['daili']) { //查看301跳转过去的网址
$Headers = curl_getinfo($ch);
if ($Headers['redirect_url']) { $ret=$Headers['redirect_url'];
} else { $ret = false; } }
curl_close($ch);
if(($paras['getcookie']||$paras['get_cookie'])&&$paras['daili']){ $ret=json_decode($ret,true);
$ret=array_merge($ret,['dl_ip'=>$dl[0]]); return $ret; } //显示代理IP
return $ret; }
使用示例:
$data=curl('https://www.yuanxiapi.cn/',[
'GetCookie'=>'1',
'nobody'=>1
]);
echo $data['code'];
其它示例我就不一一说了,懂的自然都懂。
评论
发表评论: