Check.php 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. namespace Heanup\App\Script\Controller;
  3. use Heanup\App\Api\Package\Database\Proxy;
  4. use Heanup\App\Script\Controller;
  5. class Check extends Controller
  6. {
  7. protected function main()
  8. {
  9. $_status=1;
  10. while (true){
  11. $ips=Proxy::select([],['status'=>$_status]);
  12. $_status=1-$_status;
  13. foreach ($ips as $ip) {
  14. $status=$this->check_proxy_ip_info($ip['ip'].":".$ip['port']);
  15. if (!$status){
  16. echo sprintf("%s无效,已删除",$ip['ip'] ). PHP_EOL;
  17. Proxy::update(['status'=>0], ['id'=>$ip['id']]);
  18. }else{
  19. echo sprintf("%s:%s有效",$ip['ip'],$ip['port'] ). PHP_EOL;
  20. }
  21. }
  22. echo "休眠30s" . PHP_EOL;
  23. sleep(30);
  24. }
  25. }
  26. /**
  27. * +-----------------------------------------------------------------------------
  28. * 检查代理ip信息有效性
  29. * +-----------------------------------------------------------------------------
  30. * @param string $proxy_ip [117.95.100.126:8998]
  31. * @param int $times 执行检查次数
  32. * @return bool
  33. * @author elinx <654753115@qq.com> 2016-07-29
  34. * +-----------------------------------------------------------------------------
  35. */
  36. function check_proxy_ip_info($proxy_ip = '', $times = 1)
  37. {
  38. $status = false;
  39. $header = array(
  40. // "GET / HTTP/1.1",
  41. // "HOST: www.baidu.com",
  42. "accept: application/json",
  43. "accept-encoding: gzip, deflate",
  44. "accept-language: en-US,en;q=0.8",
  45. "content-type: application/json",
  46. "user-agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.106 Safari/537.36",
  47. );
  48. $url = 'http://www.baidu.com/';
  49. $result['succeed_times'] = 0; //成功次数
  50. $result['defeat_times'] = 0; //失败次数
  51. $result['total_spen'] = 0; //总用时
  52. $ip = explode(":", $proxy_ip);
  53. for ($i = 0; $i < $times; $i++) {
  54. $curl = curl_init();
  55. curl_setopt($curl, CURLOPT_URL, $url); //设置传输的url
  56. curl_setopt($curl, CURLOPT_HTTPHEADER, $header); //发送http报头
  57. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  58. curl_setopt($curl, CURLOPT_ENCODING, 'gzip,deflate'); // 解码压缩文件
  59. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); //不验证证SSL书
  60. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); //不验证SSL证书
  61. curl_setopt($curl, CURLOPT_HTTPHEADER, array(
  62. 'Client_Ip: ' . $ip,
  63. ));
  64. curl_setopt($curl, CURLOPT_HTTPHEADER, array(
  65. 'X-Forwarded-For: ' . $ip,
  66. ));
  67. curl_setopt($curl, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
  68. curl_setopt($curl, CURLOPT_PROXY, $proxy_ip);
  69. curl_setopt($curl, CURLOPT_COOKIEFILE, dirname(__FILE__) . '/cookie.txt');
  70. curl_setopt($curl, CURLOPT_COOKIEJAR, dirname(__FILE__) . '/cookie.txt');
  71. curl_setopt($curl, CURLOPT_TIMEOUT, 10); // 设置超时限制防止死循环
  72. // $response_header = curl_getinfo($curl); // 获取返回response报头
  73. $content = curl_exec($curl);
  74. if (strstr($content, '百度一下,你就知道')) {
  75. $status = true;
  76. break;
  77. } else {
  78. $status = false;
  79. }
  80. // $result['list'][$i]['response_header'] = $response_header;
  81. }
  82. return $status;
  83. }
  84. }