用了多次curl后整合出来的,支持多种操作
/*
*url 网址
*arrip 如果有代理IP 格式array
*curlPost 需要post提交数据
*header header数据
*user_agent 模拟浏览器访问
*/
public function curl($url,$curlPost = false, $header = false,$arrip = array(),$user_agent=false)
{
$arrip = $this->arrip;
$user_agent = 'Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36';
// $header = array('X-FORWARDED-FOR:111.222.333.4', 'CLIENT-IP:111.222.333.4');
$ch = curl_init(); //初始化curl
if (substr($url, 0, 5) == 'https') {
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 跳过证书检查
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2); // 从证书中检查SSL加密算法是否存在
}
if($user_agent){
curl_setopt($ch, CURLOPT_USERAGENT, $user_agent); //模拟浏览器访问
}
if ($arrip) {
curl_setopt($ch, CURLOPT_PROXYAUTH, CURLAUTH_BASIC); //代理认证模式
curl_setopt($ch, CURLOPT_PROXY, "$arrip[0]"); //代理服务器地址
curl_setopt($ch, CURLOPT_PROXYPORT, $arrip[1]); //代理服务器端口
// curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
}
curl_setopt($ch, CURLOPT_URL, $url); //抓取指定网页
curl_setopt($ch, CURLOPT_HEADER, 0); //设置header
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //要求结果为字符串且输出到屏幕上
curl_setopt($ch, CURLOPT_TIMEOUT,5); //设置超时时间
if ($header) {
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_REFERER, "http://www.test.com");
}
// curl_setopt($ch, CURLINFO_HEADER_OUT, true);//获取httpheader所有数据
if ($curlPost) {
curl_setopt($ch, CURLOPT_POST, 1); //post提交方式
curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
}
$data = curl_exec($ch); //运行curl
if ($data == false) {
$data = "game over";
}
curl_close($ch);
return $data;
}