Server.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: chendeben
  5. * Date: 2018/8/31
  6. * Time: 下午9:35
  7. */
  8. namespace Heanup\App\Script\Controller;
  9. use Exception;
  10. use Heanup\App\Api\Package\Database\Proxy;
  11. use Heanup\App\Script\Controller;
  12. use Heanup\Library\Curl;
  13. class Server extends Controller
  14. {
  15. /**
  16. * 执行逻辑
  17. * @throws Exception
  18. */
  19. protected function main()
  20. {
  21. while (true){
  22. echo "开始获取IP" . PHP_EOL;
  23. $ips = $this->get_ip();
  24. $this->check_ip($ips);
  25. echo "休眠30s" . PHP_EOL;
  26. sleep(30);
  27. }
  28. // $ips=Proxy::select();
  29. }
  30. private function check_ip($ip_arr)
  31. {
  32. foreach ($ip_arr as $key => $value) {
  33. $response = $this->check_proxy_ip_info($value);
  34. $ips = explode(":", $value);
  35. //将结果存入数据库
  36. if ($response) {
  37. $insert = [
  38. 'ip' => $ips[0],
  39. 'port' => $ips[1],
  40. 'status' => 1
  41. ];
  42. Proxy::replace($insert, ['ip' => $ips[0], 'port' => $ips[1]]);
  43. echo $value . "\tsuccess " . PHP_EOL;
  44. } else {
  45. Proxy::update(['status' => 0], ['ip' => $ips[0], 'port' => $ips[1]]);
  46. echo $value . "\terror " . PHP_EOL;
  47. }
  48. }
  49. }
  50. //获取各大网站代理IP
  51. private function get_ip()
  52. {
  53. $ip_arr = [];
  54. echo "正在获取第一个IP列表" . PHP_EOL;
  55. $ip_arr = $this->get_xici_ip($ip_arr);
  56. echo "正在获取第二个IP列表" . PHP_EOL;
  57. $ip_arr = $this->get_kuaidaili_ip($ip_arr);
  58. return $ip_arr;
  59. }
  60. private function get_xici_ip($ip_arr)
  61. {
  62. for ($i = 1; $i <= 10; $i++) {
  63. echo sprintf("正在获取xici第%s页IP列表", $i) . PHP_EOL;
  64. $url = sprintf("http://www.xicidaili.com/nn/%s", intval($i));
  65. $infoContent = Curl::get($url);
  66. $this->convert_encoding($infoContent);
  67. preg_match_all('/<tr.*>[\s\S]*?<td class="country">[\s\S]*?<\/td>[\s\S]*?<td>(.*?)<\/td>[\s\S]*?<td>(.*?)<\/td>/', $infoContent, $match);
  68. $host_arr = $match[1];
  69. $port_arr = $match[2];
  70. foreach ($host_arr as $key => $value) {
  71. $ip_arr[] = $host_arr[$key] . ":" . $port_arr[$key];
  72. }
  73. sleep(2);
  74. }
  75. print_r($ip_arr);
  76. return $ip_arr;
  77. }
  78. private function get_kuaidaili_ip($ip_arr)
  79. {
  80. for ($i = 1; $i <= 10; $i++) {
  81. echo sprintf("正在获取kuaidaili第%s页IP列表", $i) . PHP_EOL;
  82. $url = 'https://www.kuaidaili.com/free/inha/' . $i;
  83. $infoContent = Curl::get($url);
  84. $this->convert_encoding($infoContent);
  85. preg_match_all('/<td data-title="IP">(.*?)<\/td>/', $infoContent, $host);
  86. preg_match_all('/<td data-title="PORT">(.*?)<\/td>/', $infoContent, $port);
  87. $host_arr = $host[1];
  88. $port_arr = $port[1];
  89. foreach ($host_arr as $key => $value) {
  90. $ip_arr[] = $host_arr[$key] . ":" . $port_arr[$key];
  91. }
  92. sleep(2);
  93. }
  94. print_r($ip_arr);
  95. return $ip_arr;
  96. }
  97. /**
  98. * +-----------------------------------------------------------------------------
  99. * 检查代理ip信息有效性
  100. * +-----------------------------------------------------------------------------
  101. * @param string $proxy_ip [117.95.100.126:8998]
  102. * @param int $times 执行检查次数
  103. * @return bool
  104. * @author elinx <654753115@qq.com> 2016-07-29
  105. * +-----------------------------------------------------------------------------
  106. */
  107. function check_proxy_ip_info($proxy_ip = '', $times = 1)
  108. {
  109. $status = false;
  110. $header = array(
  111. // "GET / HTTP/1.1",
  112. // "HOST: www.baidu.com",
  113. "accept: application/json",
  114. "accept-encoding: gzip, deflate",
  115. "accept-language: en-US,en;q=0.8",
  116. "content-type: application/json",
  117. "user-agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.106 Safari/537.36",
  118. );
  119. $url = 'http://www.baidu.com/';
  120. $result['succeed_times'] = 0; //成功次数
  121. $result['defeat_times'] = 0; //失败次数
  122. $result['total_spen'] = 0; //总用时
  123. $ip = explode(":", $proxy_ip);
  124. for ($i = 0; $i < $times; $i++) {
  125. $curl = curl_init();
  126. curl_setopt($curl, CURLOPT_URL, $url); //设置传输的url
  127. curl_setopt($curl, CURLOPT_HTTPHEADER, $header); //发送http报头
  128. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  129. curl_setopt($curl, CURLOPT_ENCODING, 'gzip,deflate'); // 解码压缩文件
  130. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); //不验证证SSL书
  131. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); //不验证SSL证书
  132. curl_setopt($curl, CURLOPT_HTTPHEADER, array(
  133. 'Client_Ip: ' . $ip,
  134. ));
  135. curl_setopt($curl, CURLOPT_HTTPHEADER, array(
  136. 'X-Forwarded-For: ' . $ip,
  137. ));
  138. curl_setopt($curl, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
  139. curl_setopt($curl, CURLOPT_PROXY, $proxy_ip);
  140. curl_setopt($curl, CURLOPT_COOKIEFILE, dirname(__FILE__) . '/cookie.txt');
  141. curl_setopt($curl, CURLOPT_COOKIEJAR, dirname(__FILE__) . '/cookie.txt');
  142. curl_setopt($curl, CURLOPT_TIMEOUT, 10); // 设置超时限制防止死循环
  143. // $response_header = curl_getinfo($curl); // 获取返回response报头
  144. $content = curl_exec($curl);
  145. if (strstr($content, '百度一下,你就知道')) {
  146. $status = true;
  147. break;
  148. } else {
  149. $status = false;
  150. }
  151. // $result['list'][$i]['response_header'] = $response_header;
  152. }
  153. return $status;
  154. }
  155. private function convert_encoding(&$str)
  156. {
  157. $encode = mb_detect_encoding($str, ['GB2312', 'UTF-8', 'GBK', 'ASCII']);
  158. $str = iconv($encode, 'utf-8//TRANSLIT//IGNORE', $str);
  159. }
  160. }