1<?php 2/** 3 * @Author: syyuanyizhi@163.com 4 connect refuse: errorCode 111 5 I/O timeout:errorCode 110 6 http 9510 7 tcp 9511 8 9 */ 10class Server 11{ 12 public $server; 13 14 public function run() 15 { 16 $this->server = new Swoole\Http\Server("0.0.0.0", 9508); 17 $this->server->set([ 18 'worker_num' => 1, 19 'daemonize' => true, 20 'log_file' => '/data/markyuan/swoole.log', 21 ]); 22 $this->server->on('Request', ['Server', 'onRequest']); 23 $this->server->start(); 24 } 25 26 private static function https(){ 27 //--enable-openssl 28 for($i=0;$i<2;$i++){ 29 $cli = new Swoole\Coroutine\Http\Client('0.0.0.0',443,TRUE ); 30 $cli->set([ 'timeout' => 1]); 31 $cli->setHeaders([ 32 'Host' => "api.mp.qq.com", 33 "User-Agent" => 'Chrome/49.0.2587.3', 34 'Accept' => 'text/html,application/xhtml+xml,application/xml', 35 'Accept-Encoding' => 'gzip', 36 ]); 37 $ret = ($cli->get('/cgi-bin/token?appid=3333&secret=222'.$i.$i.$i.$i.$i)); 38 error_log(__LINE__.var_export($cli,true).PHP_EOL,3,'/tmp/markyuan'); 39 $cli->close(); 40 } 41 } 42 43 private static function http(){ 44 error_log(__LINE__.'---------- begin --- http --------------'.PHP_EOL,3,'/tmp/markyuan'); 45 for($i=0;$i<2;$i++){ 46 $cli = new Swoole\Coroutine\Http\Client('0.0.0.0', 9510); 47 $cli->set([ 'timeout' => 1]); 48 $cli->setHeaders([ 49 'Host' => "api.mp.qq.com", 50 "User-Agent" => 'Chrome/49.0.2587.3', 51 'Accept' => 'text/html,application/xhtml+xml,application/xml', 52 'Accept-Encoding' => 'gzip', 53 ]); 54 error_log(__LINE__.var_export($cli,true).PHP_EOL,3,'/tmp/markyuan'); 55 $ret = ($cli->get('/cn/token?appid=1FxxxxS9V'.$i.$i.$i.$i.$i)); 56 error_log(__LINE__.var_export($ret,true).PHP_EOL,3,'/tmp/markyuan'); 57 error_log(__LINE__.var_export($cli,true).PHP_EOL,3,'/tmp/markyuan'); 58 $cli->close(); 59 } 60 error_log(__LINE__.'---------- end --- http --------------'.PHP_EOL,3,'/tmp/markyuan'); 61 62 } 63 64 private static function multihttp(){ 65 66 error_log(__LINE__.'---------- begin --- multi --------------'.PHP_EOL,3,'/tmp/markyuan'); 67 68 $cliAA= new Swoole\Coroutine\Http\Client('0.0.0.0', 9510); 69 $cliAA->set(['timeout' => 1]); 70 $cliAA->setHeaders([ 71 'Host' => "api.mp.qq.com", 72 "User-Agent" => 'Chrome/49.0.2587.3', 73 ]); 74 $cliBB= new Swoole\Coroutine\Http\Client('0.0.0.0', 9510); 75 $cliBB->set([ 'timeout' => 1]);// 76 $cliBB->setHeaders([ 77 'Host' => "api.mp.qq.com", 78 "User-Agent" => 'Chrome/49.0.2587.3', 79 ]); 80 error_log(__LINE__.var_export($cliAA,true).PHP_EOL,3,'/tmp/markyuan'); 81 error_log(__LINE__.var_export($cliBB,true).PHP_EOL,3,'/tmp/markyuan'); 82 $retAA=$cliAA->setDefer(1); 83 $retBB=$cliBB->setDefer(1); 84 error_log(__LINE__.var_export($retAA,true).PHP_EOL,3,'/tmp/markyuan'); 85 error_log(__LINE__.var_export($retBB,true).PHP_EOL,3,'/tmp/markyuan'); 86 error_log(__LINE__.var_export($cliAA,true).PHP_EOL,3,'/tmp/markyuan'); 87 error_log(__LINE__.var_export($cliBB,true).PHP_EOL,3,'/tmp/markyuan'); 88 $retAA = ($cliAA->get('/cn/token?appid=AAA')); 89 $retBB = ($cliBB->get('/cn/token?appid=BBB')); 90 error_log(__LINE__.var_export($retAA,true).PHP_EOL,3,'/tmp/markyuan'); 91 error_log(__LINE__.var_export($retBB,true).PHP_EOL,3,'/tmp/markyuan'); 92 error_log(__LINE__.var_export($cliAA,true).PHP_EOL,3,'/tmp/markyuan'); 93 error_log(__LINE__.var_export($cliBB,true).PHP_EOL,3,'/tmp/markyuan'); 94 $retAA=$cliAA->recv(); 95 $retBB=$cliBB->recv(); 96 error_log(__LINE__.var_export($retAA,true).PHP_EOL,3,'/tmp/markyuan'); 97 error_log(__LINE__.var_export($retBB,true).PHP_EOL,3,'/tmp/markyuan'); 98 error_log(__LINE__.var_export($cliAA,true).PHP_EOL,3,'/tmp/markyuan'); 99 error_log(__LINE__.var_export($cliBB,true).PHP_EOL,3,'/tmp/markyuan'); 100 $retAA=$cliAA->close(); 101 $retBB=$cliBB->close(); 102 error_log(__LINE__.'---------- end --- multi --------------'.PHP_EOL,3,'/tmp/markyuan'); 103 } 104 105 106 107 private static function tcp(){ 108 for($i=0;$i<2;$i++){ 109 $tcp_cli = new Swoole\Coroutine\Client(SWOOLE_SOCK_TCP); 110 $ret = $tcp_cli ->connect("0.0.0.0", 9511); 111 $ret = $tcp_cli ->send('test for the coro'); 112 $ret = $tcp_cli ->recv(); 113 $ret=$tcp_cli->close(); 114 } 115 } 116 117 private static function coro_dns(){ 118 swoole_async_set(array('use_async_resolver'=>1)); 119 swoole_async_set(array('dns_cache_refresh_time'=>0)); 120 $ret=swoole_async_dns_lookup_coro("www.baidu.com",0.5); 121 error_log(' ip and host '.$host.print_r($ret,true),'3','/home/yuanyizhi/markyuan/markyuan.log'); 122 return $ret; 123// swoole_async_dns_lookup("www.baidu.com", function($host, $ip){ 124// error_log(' ip and host '.$host.' and ip '.$ip,'3','/home/yuanyizhi/markyuan/markyuan.log'); 125// }); 126 } 127 128 129private static function tcpmulti(){ 130 $cliAA = new Swoole\Coroutine\Client(SWOOLE_SOCK_TCP); 131 $cliBB = new Swoole\Coroutine\Client(SWOOLE_SOCK_TCP); 132 $retAA = $cliAA ->connect("0.0.0.0", 9511); 133 $retBB = $cliBB ->connect("0.0.0.0", 9511); 134 $retAA = $cliAA ->send('test for the coro'); 135 $retBB = $cliBB ->send('test for the coro'); 136 $retAA = $cliAA->recv(); 137 $retBB = $cliBB->recv(); 138 $cliAA->close(); 139 $cliBB->close(); 140 } 141 142 public static function onRequest($request, $response) 143 { 144// self::multihttp(); 145// self::http(); 146 //self::https(); 147// self::tcp(); 148 // self::tcpmulti(); 149 $ret=self::coro_dns(); 150 $response->end(print_r($ret,true)); 151 } 152 153 154 public static function staticFunc() 155 { 156 echo "in static function"; 157 } 158} 159 160$server = new Server(); 161 162$server->run(); 163