您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

curl类封装

2024/4/23 18:04:54发布14次查看
ch = curl_init(); $this->url = $url ? $url : $this->url; //$this->set_useragent = $_server['http_user_agent']; // 模拟用户使用的浏览器 $this->set_useragent =mozilla/5.0 (iphone; cpu iphone os 6_1_4 like mac os x) applewebkit/536.26 (khtml, like gecko) version/7.0 mobile/10b350 safari/9537.53; // $this->set_useragent = mozilla/5.0 (windows nt 6.1; wow64) applewebkit/537.36 (khtml, like gecko) chrome/36.0.1985.143 safari/537.36; //$this->cookie_file=dirname(__file__)./cookie_.md5(basename(__file__))..txt; //初始化cookie文件路径 //$this->cookie_file= sae_tmp_path.tmpfs; $this->cookie_file = saekv://cookie_2014.txt; } //关闭curl public function close(){ curl_close($this->ch); } //析构函数 public function __destruct(){ $this->close(); } //设置超时 public function set_time_out($timeout=20){ if(intval($timeout) != 0) $this->set_time_out = $timeout; return $this; } //设置来源页面 public function set_referer($referer = ){ if (!empty($referer)) curl_setopt($this->ch, curlopt_referer , $referer); return $this; } //设置cookie存放模式 1客户端、2服务器文件 public function set_cookie_mode($mode = ){ $this->cookie_mode = $mode; return $this; } //载入cookie public function load_cookie(){ if($this->cookie_mode == 1 ) { if(isset($_cookie['curl'])){ curl_setopt($this->ch,curlopt_cookie,$_cookie['curl']); }else{ $this->exec(); curl_setopt($this->ch,curlopt_cookie,$this->cookie_file); } } if($this->cookie_mode == 2 ) { curl_setopt($this->ch, curlopt_cookiefile , $this->cookie_file); } if($this->cookie_mode == 3 ) { $kv = new saekv(); $ret = $kv->init(); $ret = $kv->get('curl_cookie'); if($ret) curl_setopt($this->ch,curlopt_cookie, $ret); } return $this; } //设置保存cookie方式 $cookie_val 模式1为变量 模式2为文件路径 public function save_cookie($cookie_val = ) { //保存在客户端 if($this->cookie_mode == 1 && $cookie_val){ setcookie('curl',$cookie_val); } //保存服务器端 if($this->cookie_mode == 2){ if(!empty($cookie_val)) $this->cookie_file = $cookie_val; curl_setopt($this->ch, curlopt_cookiejar , $this->cookie_file); } //保存在sae if($this->cookie_mode == 3 && $cookie_val){ $kv = new saekv(); $ret = $kv->init(); $ret = $kv->get('curl_cookie'); if($ret){ $ret = $kv->set('curl_cookie', $cookie_val ); }else{ $ret = $kv->add('curl_cookie', $cookie_val); } } return $this; } //post参数 (array) $post public function post ($post = ){ if($post && is_array($post)){ curl_setopt($this->ch, curlopt_post , 1); curl_setopt($this->ch, curlopt_postfields , $post ); } return $this; } //设置代理 ,例如'68.119.83.81:27977' public function set_proxy($proxy = ){ if($proxy){ curl_setopt($this->ch, curlopt_proxytype, curlproxy_socks5); curl_setopt($this->ch, curlopt_proxy,$proxy); } return $this; } //设置伪造ip public function set_ip($ip=){ if(!empty($ip)) curl_setopt($this->ch, curlopt_httpheader, array(x-forwarded-for:$ip, client-ip:$ip)); return $ip; } //设置是否显示返回头信息 public function show_header($show=0){ $this->show_header = 0; if($show) $this->show_header = 1; return $this; } //设置请求头信息 public function set_useragent($str=){ if($str) $this->set_useragent = $str; return $this; } //执行 public function exec ($url = ){ if(!$url) $url = $this->url; curl_setopt($this->ch, curlopt_url, $url); // 要访问的地址 curl_setopt($this->ch, curlopt_ssl_verifypeer, 0); // 对认证证书来源的检查 curl_setopt($this->ch, curlopt_returntransfer , 1 ); //获取的信息以文件流的形式返回 curl_setopt($this->ch, curlopt_ssl_verifyhost, 1); // 从证书中检查ssl加密算法是否存在 curl_setopt($this->ch, curlopt_useragent, $this->set_useragent); // 模拟用户使用的浏览器 curl_setopt($this->ch, curlopt_followlocation, 1); // 使用自动跳转 curl_setopt($this->ch, curlopt_autoreferer, 1); // 自动设置referer curl_setopt($this->ch, curlopt_timeout, $this->set_time_out); //超时设置 curl_setopt($this->ch, curlopt_header, $this->show_header); // 显示返回的header区域内容 curl_setopt($this->ch, curlopt_nobody, 0);//不返回response body内容 $res = curl_exec($this->ch); $this->flag_if_have_run = true; if (curl_errno($this->ch)) { //echo 'errno'.curl_error($this->ch); return false; } if($this->show_header == 1){ //数组形式返回头信息和body信息 list($header, $body) = explode(\r\n\r\n, $res); $arr['header'] = $header; $arr['body'] = $body; if($this->cookie_mode == 1 || $this->cookie_mode == 3){ preg_match_all(/set\-cookie:([^\r\n]*)/i, $header, $matches); //print_r($matches); if($matches && isset($matches[1]) ){ $val = implode(';',array_unique(explode(';',implode(';',$matches[1])))); //去重处理 if($val) $this->save_cookie($val); //设置客户端保存cookie } } if($arr) return $arr; } return $res; } //返回 curl_getinfo信息 public function get_info(){ if($this->flag_if_have_run == true ) return curl_getinfo($this->ch); else throw new exception(需先运行( 执行exec ),再获取信息); } } ?>
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product