weather_server.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. <?php
  2. error_reporting(E_ALL);
  3. ini_set('display_errors', '1');
  4. ini_set('memory_limit', '-1');
  5. class HttpServ
  6. {
  7. public $http;
  8. public $setting = array();
  9. public function __construct()
  10. {
  11. }
  12. public function set($setting)
  13. {
  14. $this->setting = $setting;
  15. }
  16. public function init()
  17. {
  18. $this->http = new swoole_http_server($this->setting['host'], $this->setting['port'], SWOOLE_BASE);
  19. $this->http->set($this->setting);
  20. //register_shutdown_function('handleFatal');
  21. $this->http->on('request', function ($request, $response)
  22. {
  23. if ($request->server['request_uri'] == '/favicon.ico')
  24. {
  25. $response->status(404);
  26. $response->end('Not Found');
  27. return;
  28. }
  29. $this->getResult($response);
  30. });
  31. }
  32. function getResult2($response)
  33. {
  34. $client = new swoole_client(SWOOLE_SOCK_TCP, SWOOLE_SOCK_ASYNC);
  35. $client->on('connect', function ($cli)
  36. {
  37. echo "cli1 connect\n";
  38. $cli->send("hello world\n");
  39. });
  40. $client->on('Receive', function ($cli, $data) use ($response)
  41. {
  42. echo "cli1 receive\n";
  43. $response->end($data);
  44. $cli->close();
  45. });
  46. $client->on("error", function ($cli) use ($response)
  47. {
  48. echo "cli1 error\n";
  49. $response->end("empty\n");
  50. });
  51. $client->on("close", function ($cli)
  52. {
  53. echo "cli1 close\n";
  54. });
  55. $client->connect('127.0.0.1', 9501);
  56. }
  57. function getResult5($response)
  58. {
  59. swoole_async_dns_lookup("weather.gtimg.cn", function ($host, $ip) use ($response)
  60. {
  61. $response->header('Content-Type', 'application/json');
  62. $response->write(json_encode(array($host => $ip)));
  63. $response->end();
  64. });
  65. }
  66. function getResult3($response)
  67. {
  68. $cityId = '01010101';
  69. // swoole_async_dns_lookup("weather.gtimg.cn", function ($host, $ip) use ($cityId, $response)
  70. // {
  71. // if (empty($ip))
  72. // {
  73. // return $ret;
  74. // }
  75. // else
  76. // {
  77. $ip = '14.18.245.236';
  78. $httpcli = new swoole_http_client($ip, 80);
  79. //$httpcli->on("close", function($httpcli){});
  80. $url = "/qqindex/" . $cityId . ".js?_ref=14";
  81. $httpcli->get($url, function ($hcli) use ($response)
  82. {
  83. //echo "get content is" . $hcli->body;
  84. $retWeather = iconv("GBK", 'UTF-8', $hcli->body);
  85. //echo "ret:" . $retWeather;
  86. $hcli->close();
  87. $response->header('Content-Type', 'application/json');
  88. $response->write(json_encode($retWeather));
  89. $response->end();
  90. });
  91. // }
  92. // });
  93. }
  94. function getResult($response)
  95. {
  96. $client = new swoole_redis();
  97. $ip = "127.0.0.1";
  98. $port = 6379;
  99. $client->connect($ip, $port, function (swoole_redis $client, $result) use ($response)
  100. {
  101. if ($result === false)
  102. {
  103. echo "connect to redis server failed\n";
  104. return false;
  105. }
  106. $client->GET('test', function (swoole_redis $client, $result) use ($response)
  107. {
  108. //echo "get result is :" . $result;
  109. $client->close();
  110. $cityId = '01010101';
  111. swoole_async_dns_lookup("weather.gtimg.cn", function ($host, $ip) use ($cityId, $response)
  112. {
  113. if (empty($ip))
  114. {
  115. return false;
  116. }
  117. else
  118. {
  119. $httpcli = new swoole_http_client($ip, 80);
  120. //$httpcli->on("close", function($httpcli){});
  121. $url = "/qqindex/" . $cityId . ".js?_ref=14";
  122. $httpcli->get($url, function ($hcli) use ($response)
  123. {
  124. //echo "get content is" . $hcli->body;
  125. $retWeather = iconv("GBK", 'UTF-8', $hcli->body);
  126. //echo "ret:" . $retWeather;
  127. $hcli->close();
  128. $response->header('Content-Type', 'application/json');
  129. $response->write(json_encode($retWeather));
  130. $response->end();
  131. });
  132. }
  133. });
  134. });
  135. });
  136. }
  137. function getResult4($response)
  138. {
  139. $client = new swoole_redis();
  140. $ip = "127.0.0.1";
  141. $port = 6379;
  142. $client->connect($ip, $port, function (swoole_redis $client, $result) use ($response)
  143. {
  144. if ($result === false)
  145. {
  146. echo "connect to redis server failed\n";
  147. return false;
  148. }
  149. $client->GET('key', function (swoole_redis $client, $result) use ($response)
  150. {
  151. //echo "get result is :" . $result;
  152. $response->header('Content-Type', 'application/json');
  153. $response->end($result);
  154. });
  155. });
  156. }
  157. public function start()
  158. {
  159. $this->init();
  160. $this->http->start();
  161. }
  162. }
  163. $setting = array(
  164. 'host' => '127.0.0.1',
  165. 'port' => 9100,
  166. 'worker_num' => 1,
  167. 'dispatch_mode' => 2,
  168. //'reactor_num' => 4,
  169. 'daemonize' => 0,
  170. //'log_file' => './logs/test_udp_server.log',
  171. );
  172. $server = new HttpServ();
  173. $server->set($setting);
  174. $server->start();