您好,欢迎访问一九零五行业门户网

多线程的cURL

本处是一个可以灵活的多线程的调用 curl的。
这里跟php手册 http://us2.php.net/manual/zh/function.curl-multi-select.php 中提供的样例不同,代码执行效率要高不少
本处两个文件,一个是muti_curl的文件,里面包含两个class
一个是运用的方法,这里是批量检查代理ip是否可用 url = $url; $this->method = $method; $this->post_data = $post_data; $this->headers = $headers; $this->options = $options; } public function __destruct() { unset($this->url, $this->method, $this->post_data, $this->headers, $this->options); }}/*********************************************************************************************** 批量操作的类*******************************************************************************************/class muti_curl { protected $thread_size = 100; protected $timeout = 30; private $callback; protected $options = array( curlopt_ssl_verifypeer => false,//禁用后curl将终止从服务端进行验证。使用curlopt_cainfo选项设置证书使用curlopt_capath选项设置证书目录 如果curlopt_ssl_verifypeer(默认值为2)被启用,curlopt_ssl_verifyhost需要被设置成true否则设置为false。 自curl 7.10开始默认为true。从curl 7.10开始默认绑定安装。 curlopt_returntransfer => true, //将 curl_exec()获取的信息以文件流的形式返回,而不是直接输 curlopt_connecttimeout => 15, curlopt_timeout => 30,// curlopt_http_version=>curl_http_version_1_0, //使用代理的时候用这个去抓取数据,更爽// curlopt_autoreferer=>false,// 当根据location:重定向时,自动设置header中的referer:信息。 // curlopt_binarytransfer=>false, //在启用curlopt_returntransfer的时候,返回原生的(raw)输出这个不用设置。 // curlopt_cookiesession=>true,// 启用时curl会仅仅传递一个session cookie,忽略其他的cookie,默认状况下curl会将所有的cookie返回// curlopt_crlf=>false,// 启用时将unix的换行符转换成回车换行符。 // curlopt_dns_use_global_cache=>false, // 启用时会启用一个全局的dns缓存,此项为线程安全的,并且默认启用。 // curlopt_failonerror=>false, // 显示http状态码,默认行为是忽略编号小于等于400的http信息。 // curlopt_filetime=>true, //启用时会尝试修改远程文档中的信息。结果信息会通过 curl_getinfo()函数的curlinfo_filetime选项返回。 curl_getinfo(). // curlopt_followlocation=>false, // 启用时会将服务器服务器返回的location: 放在header中递归的返回给服务器,使用curlopt_maxredirs可以限定递归返回的数量。 // curlopt_forbid_reuse=>true, //在完成交互以后强迫断开连接,不能重用。 // curlopt_fresh_connect=>true,// 强制获取一个新的连接,替代缓存中的连接。 // curlopt_ftp_use_eprt=>false,// 启用时当ftp下载时,使用eprt (或 lprt)命令。设置为false时禁用eprt和lprt,使用port命令 only. // curlopt_ftp_use_epsv=>false,// 启用时,在ftp传输过程中回复到pasv模式前首先尝试epsv命令。设置为false时禁用epsv命令。 // curlopt_ftpappend=>false,// 启用时追加写入文件而不是覆盖它。 // curlopt_ftpascii=>false,// curlopt_transfertext的别名。 // curlopt_ftplistonly=>false,// 启用时只列出ftp目录的名字。 // curlopt_header=>true,// 启用时会将头文件的信息作为数据流输出。 // curlinfo_header_out=>false, //启用时追踪句柄的请求字符串。 // curlopt_httpget=>true,// 启用时会设置http的method为get,因为get是默认是,所以只在被修改的情况下使用。 // curlopt_httpproxytunnel =>true,// 启用时会通过http代理来传输。 // curlopt_mute=>true,// 启用时将curl函数中所有修改过的参数恢复默认值。 // curlopt_netrc=>false,// 在连接建立以后,访问~/.netrc文件获取用户名和密码信息连接远程站点。 // curlopt_nobody=>true, 启用时将不对html中的body部分进行输出。 // curlopt_noprogress=>false,//启用时关闭curl传输的进度条,此项的默认设置为启用。// curlopt_nosignal=>false,// 启用时忽略所有的curl传递给php进行的信号。在sapi多线程传输时此项被默认启用。 curl 7.10时被加入。// curlopt_post=>false,// 启用时会发送一个常规的post请求,类型为:application/x-www-form-urlencoded,就像表单提交的一样。 // curlopt_put=>false,// 启用时允许http发送文件,必须同时设置curlopt_infile和curlopt_infilesize。 // curlopt_transfertext=>false,// 启用后对ftp传输使用ascii模式。对于ldap,它检索纯文本信息而非html。在windows系统上,系统不会把stdout设置成binary模式。 // curlopt_unrestricted_auth=>true,// 在使用curlopt_followlocation产生的header中的多个locations中持续追加用户名和密码信息,即使域名已发生改变。 // curlopt_upload=>false,// 启用后允许文件上传。 // curlopt_verbose =>true,// 启用时会汇报所有的信息,存放在stderr或指定的curlopt_stderr中。 ); private $headers = array(); private $requests = array(); private $requestmap = array(); /********************* 构造一个callback的函数 ********************/ function __construct($callback = null) { $this->callback = $callback; } /******************************************************************** 重载 __get的方法 *******************************************************************/ public function __get($name) { return (isset($this->{$name})) ? $this->{$name} : null; } /********************************************************************* 重载 __set的方法 *******************************************************/ public function __set($name, $value) { // 增加一个设置到headers if ($name == options || $name == headers) { $this->{$name} = $value + $this->{$name}; } else { $this->{$name} = $value; } return true; } //增加一个请求 public function add($request) { $this->requests[] = $request; return true; } public function request($url, $method = get, $post_data = null, $headers = null, $options = null) { $this->requests[] = new request_setting($url, $method, $post_data, $headers, $options); return true; } public function get($url, $headers = null, $options = null) { return $this->request($url, get, null, $headers, $options); } public function post($url, $post_data = null, $headers = null, $options = null) { return $this->request($url, post, $post_data, $headers, $options); } private function single_curl() { $ch = curl_init(); //初始化 $request = array_shift($this->requests);//把第一个单元移出并作为结果 $options = $this->get_options($request);//获得该单元的设置 curl_setopt_array($ch, $options);//批设置 $output = curl_exec($ch); $curl_info = curl_getinfo($ch); if ($this->callback) { $callback = $this->callback; if (is_callable($this->callback)) { call_user_func($callback, $output, $curl_info, $request); } } else return $output; return true; } private function rolling_curl($thread_size = null) { if ($thread_size){ $this->thread_size = $thread_size; } if (count($this->requests) thread_size){ $this->thread_size = count($this->requests); } if ($this->thread_size $errorinfo = '线程大小必须大于 1!!!!'; throw new exception($errorinfo); } $queue = curl_multi_init(); //在线程里开始增加任务队列 for ($i = 0; $i thread_size; $i++) { $ch = curl_init(); $options = $this->get_options($this->requests[$i]); curl_setopt_array($ch, $options);//获得设置 curl_multi_add_handle($queue, $ch);//添加进去 $key = (string) $ch; $this->requestmap[$key] = $i; } do { while (($statu_run_muti_exec = curl_multi_exec($queue, $active)) == curlm_call_multi_perform) ; if ($statu_run_muti_exec != curlm_ok){ break; } // 发现完成的一个请求,进行处理 while ($done = curl_multi_info_read($queue)) { $curl_info = curl_getinfo($done['handle']); $output = curl_multi_getcontent($done['handle']); $callback = $this->callback; if (is_callable($callback)){ $key = (string) $done['handle']; $request = $this->requests[$this->requestmap[$key]]; unset($this->requestmap[$key]);//这个销毁变量用得很帅 call_user_func($callback, $output, $curl_info, $request); } //增加一个未处理的请求加入到一个已经完成的队列中 if ($i requests) && isset($this->requests[$i]) && $i requests)) { $ch = curl_init(); $options = $this->get_options($this->requests[$i]); curl_setopt_array($ch, $options); curl_multi_add_handle($queue, $ch); $key = (string) $ch; $this->requestmap[$key] = $i; $i++; } curl_multi_remove_handle($queue, $done['handle']); echo done ; print_r($queue); print_r ($done); } // 这一步非常非常重要如果有一个完成后,要重新设置设置timeout的时间 //这里很关键的一点,就是要保证第一次所有的线程里的请求最少有一个是有效的,否则就出现第一次所有的都没有效果,导致 $active=0,因此不执行下面的东西 if ($active >0){ curl_multi_select($queue, $this->timeout); } } while ($active); curl_multi_close($queue); return true; } public function execute($thread_size = null) { //判断thread_size的大小如果只有一个请求,就用单线程模式 if (count($this->requests) == 1) { return $this->single_curl(); } else { return $this->rolling_curl($thread_size); } } private function get_options($request) { $options = $this->__get('options'); if (ini_get('safe_mode') == 'off' || !ini_get('safe_mode')) {// $options[curlopt_followlocation] = 1;// $options[curlopt_maxredirs] = 5; } $headers = $this->__get('headers'); if ($request->options) { $options = $request->options + $options; } $options[curlopt_url] = $request->url; //下面分别对post选项与header选项进行设置 if ($request->post_data){ $options[curlopt_post] = 1; $options[curlopt_postfields] = $request->post_data; } if ($headers) { $options[curlopt_header] = 0; $options[curlopt_httpheader] = $headers; } return $options; } public function __destruct() { unset($this->thread_size, $this->callback, $this->options, $this->headers, $this->requests); }}?>
复制代码
=1 ) { // $result = true; echo true
;// echo $request[options][10004];// print_r ($request->options); echo $request->options[curlopt_proxy]; $good_proxy[]=$request->options[curlopt_proxy]; } echo '
the-->'. $sucesesnum.'// print_r ($request); //echo $request->url; $sucesesnum++; echo ;}$params = array_merge($_get, $_post); //此处获得传递过来的代理ip的地址$result = $proxy_ip = trim($params['ip']);$timeout=intval(trim($params['timeout']));if($timeoutif($timeout>300){$timeout=300;}$thread_size=intval(trim($params['thread_size']));if($thread_sizeif($thread_size>300){$thread_size =300;}if($proxy_ip == '') { echo '请输入ip!!'; return;}$replace_arr1 = array(' ', 'qq代理:', 'dn28.com', 'qqip', 'qq代理', 'qq代理ip', '代理ip:', 'ip:', '代理ip','',','\\','/',' ');$result = str_replace($replace_arr1, array(''), $result);$result = str_replace(,, \n, $result);$resarr = explode(\n, $result);foreach($resarr as $k => $v) { $posproxy = getpos($v, '@'); if($posproxy===false){ if (!empty($v)){$proxyip_and_port = $v; } }else{ $proxyip_and_port = substr($v, 0, $posproxy); } $newres[] =trim($proxyip_and_port);}print_r($newres);//die();$option_setting = array( curlopt_ssl_verifypeer => 0, curlopt_returntransfer => true, curlopt_connecttimeout => 5, curlopt_timeout => 30, curlopt_header=>false, curlopt_proxy=>'',//这个地方设置代理的位置 );$url= 'http://www.baidu.com/robots.txt';$btime=time();$rc = new muti_curl(request_callback);$rc->timeout = $timeout;$rc->thread_size = $thread_size;foreach ($newres as $v) { $option_setting[curlopt_proxy]=$v; $request = new request_setting($url, $method = get, $post_data = null,$header= null, $option_setting); $rc->add($request);}$rc->execute();$etime=time();$usedtime=$etime-$btime;echo 'all'. $sucesesnum.'use'. $usedtime; echo '';$good_proxy= array_unique($good_proxy);$str='';foreach ($good_proxy as $v){ $str.='.trim($v).',;}$str= str_replace ( ' ' , '' ,$str );$str = preg_replace('/\s+/', ' ', $str);echo $str.'
';var_export ($good_proxy);//var_dump ($good_proxy);//**************************************************************************************************//*******************************只用了一个函数function parseproxyinfo ( $proxystr ) { //$proxystr = '202.115.207.25:80@http;四川省成都市 四川师范大学'; $posip = getpos($proxystr, ':'); $ip = substr($proxystr, 0, $posip); $posport = getpos($proxystr, '@'); $port = substr($proxystr, $posip+1, $posport-$posip-1); $postype = getpos($proxystr, ';'); $type = substr($proxystr, $posport+1, $postype-$posport-1); $location = substr(strstr($proxystr, ';'), 1); return array( 'ip' => $ip, 'port' => $port, 'type' => $type, 'location' => $location );}function getpos($haystack, $needle){ return strpos($haystack, $needle);}function check_proxy_is_useful($model, $proxy_info_arr = array()) { global $params, $config; if($model == 'single') { $proxy_port = intval(trim($params['port'])); $check_proxy_url = $config['verify_url']; $proxy_time_out = intval(trim($params['timeout'])); $retry = intval(trim($params['retry'])); $proxy_ip = trim($params['ip']); $proxy = new proxy( $proxy_ip, $proxy_port, $check_proxy_url, $proxy_time_out, $retry ); //成功返回string success, 失败返回boolean false $result = $proxy -> check_proxy(); //var_dump($result); $proxy_str_success = ''.$proxy_ip.':'.$proxy_port.'@'.'http 代理验证成功!'; $proxy_str_failed = ''.$proxy_ip.':'.$proxy_port.'@'.'http 代理验证失败!'; return $result !== false ? $proxy_str_success : $proxy_str_failed; } elseif ($model == 'collect') { $proxy_port = intval(trim($proxy_info_arr['port'])); $check_proxy_url = $config['verify_url']; $proxy_time_out = intval(trim($params['timeout'])); $retry = intval(trim($params['retry'])); $proxy_ip = trim($proxy_info_arr['ip']); /*echo $proxy_ip.'
'; echo $proxy_port.'
'; echo $check_proxy_url.'
'; echo $proxy_time_out.'
'; echo $retry.'
';*/ if(!isset($proxy)) { $proxy = new proxy( $proxy_ip, $proxy_port, $check_proxy_url, $proxy_time_out, $retry ); } //成功返回string success, 失败返回boolean false $result = $proxy -> check_proxy(); return $result; }}function get_single(){ global $params, $config; $proxy_ip = trim($params['ip']); if($proxy_ip == '') { echo '请输入ip!!'; return; } echo check_proxy_is_useful('single');}function get_proxy_by_collect(){ global $params, $config; $params['url'] = trim($params['url']); if($params['url'] == '') { echo '请输入url!'; return; } //$url = 'http://www.dn28.com/html/75/n-5175.html'; $con = iconv('gbk', 'utf-8', file_get_contents($params['url'])); preg_match ('/(.*)
/s', $con, $arr); $result = strip_tags ($arr[1], '
'); $replace_arr1 = array(' ', 'qq代理:', 'dn28.com', 'qqip', 'qq代理', 'qq代理ip', '代理ip:', 'ip:', '代理ip'); $result = str_replace($replace_arr1, array(''), $result); //print_r($arr); $resarr = explode('
', $result); //print_r($resarr); echo '代理开始批量验证中,整个过程将会花费您几分钟时间。'; unset($_session['success_arr']); foreach($resarr as $k => $v) { $newres[$k] = parseproxyinfo($v); //print_r($newres[$k]); /*return;*/ $result = check_proxy_is_useful('collect', $newres[$k]); $proxy_str_success = ''.$newres[$k]['ip'].':'.$newres[$k]['port'].'@'.$newres[$k]['type'].' 代理验证成功!   ip地址:'.$newres[$k]['location'].''; $proxy_str_failed = ''.$newres[$k]['ip'].':'.$newres[$k]['port'].'@'.$newres[$k]['type'].' 代理验证失败!   ip地址:'.$newres[$k]['location'].''; if($result !== false ){ echo $proxy_str_success; $_session['success_arr'][] = $success_arr[] = $newres[$k]; } else { echo $proxy_str_failed; } echo '
'; } if(isset($success_arr) && count($success_arr) > 0 ) { save_success_proxy($success_arr); echo '[保存验证成功的代理到本地电脑]   [我要看看历史数据]
'; } else { echo '[我要看看历史数据]
'; } //print_r($success_arr);}function get_proxy_by_rule(){ global $params, $config; $result = $proxy_ip = trim($params['ip']); if($proxy_ip == '') { echo '请输入ip!!'; return; } $replace_arr1 = array(' ', 'qq代理:', 'dn28.com', 'qqip', 'qq代理', 'qq代理ip', '代理ip:', 'ip:', '代理ip'); $result = str_replace($replace_arr1, array(''), $result); $resarr = explode(\n, $result); //print_r($resarr); echo '代理开始批量验证中,整个过程将会花费您几分钟时间。'; unset($_session['success_arr']); foreach($resarr as $k => $v) { $newres[$k] = parseproxyinfo($v); //print_r($newres[$k]); /*return;*/ $result = check_proxy_is_useful('collect', $newres[$k]); //var_dump($result); $proxy_str_success = ''.$newres[$k]['ip'].':'.$newres[$k]['port'].'@'.$newres[$k]['type'].' 代理验证成功!   ip地址:'.$newres[$k]['location'].''; $proxy_str_failed = ''.$newres[$k]['ip'].':'.$newres[$k]['port'].'@'.$newres[$k]['type'].' 代理验证失败!   ip地址:'.$newres[$k]['location'].''; if($result !== false ){ echo $proxy_str_success; $_session['success_arr'][] = $success_arr[] = $newres[$k]; } else { echo $proxy_str_failed; } echo '
'; } if(isset($success_arr) && count($success_arr) > 0 ) { save_success_proxy($success_arr); echo '[保存到php格式文件]   [保存验证成功的代理到本地电脑]   [我要看看历史数据]
'; } else { echo '[我要看看历史数据]
'; }}function save_success_proxy($success_arr){ global $config; date_default_timezone_set('prc'); $str = ''; foreach($success_arr as $k => $v) { $str .= $v['ip'].':'.$v['port'].'@'.$v['type'].';'.$v['location'].\n; } $fp = fopen($config['root_path'].'/success_proxy/'.date('ymdhi').'.log', 'a+'); fwrite($fp, $str); fclose($fp); unset($str);}?>
复制代码
其它类似信息

推荐信息