示例#1
0
 public static function proxy_test()
 {
     $options = array(CURLOPT_URL => 'http://www.baidu.com', CURLOPT_REFERER => 'http://www.baidu.com', CURLOPT_USERAGENT => 'Mozilla/5.0 (compatible; Baiduspider/2.0; +http://www.baidu.com/search/spider.html)', CURLOPT_TIMEOUT => 10, CURLOPT_CONNECTTIMEOUT => 8, CURLOPT_RETURNTRANSFER => 1, CURLOPT_HEADER => 0, CURLOPT_NOSIGNAL => true, CURLOPT_DNS_USE_GLOBAL_CACHE => true, CURLOPT_DNS_CACHE_TIMEOUT => 86400, CURLOPT_SSL_VERIFYPEER => false, CURLOPT_SSL_VERIFYHOST => false);
     if (empty(spider::$proxy_array)) {
         if (empty(spider::$curl_proxy)) {
             return false;
         }
         spider::$proxy_array = explode("\n", spider::$curl_proxy);
         // socks5://127.0.0.1:1080@username:password
     }
     if (empty(spider::$proxy_array)) {
         return false;
     }
     $rand_keys = array_rand(spider::$proxy_array, 1);
     $proxy = spider::$proxy_array[$rand_keys];
     $proxy = trim($proxy);
     $options = spiderTools::proxy($options, $proxy);
     $ch = curl_init();
     curl_setopt_array($ch, $options);
     curl_exec($ch);
     $info = curl_getinfo($ch);
     curl_close($ch);
     if ($info['http_code'] == 200) {
         return $proxy;
     } else {
         unset(spider::$proxy_array[$rand_keys]);
         return spiderTools::proxy_test();
     }
 }