Example #1
0
 public function request($method, $uri, $data = array())
 {
     $request = array("method" => $method, "uri" => $uri, "data" => $data);
     if ($this->_swClient) {
         $this->_request($request);
     } else {
         $self = $this;
         \swoole_async_dns_lookup($this->_host, function ($host, $ip) use($self, $request) {
             $self->_swClient = new \swoole_http_client($ip, $self->_port);
             $self->_request($request);
         });
     }
     return $this;
 }
Example #2
0
 public static function request(callable $callback, $url, $method = 'GET', array $headers = [], array $params = [])
 {
     $parsed_url = parse_url($url);
     \swoole_async_dns_lookup($parsed_url['host'], function ($host, $ip) use($parsed_url, $callback, $url, $method, $headers, $params) {
         $port = isset($parsed_url['port']) ? $parsed_url['port'] : 'https' == $parsed_url['scheme'] ? 443 : 80;
         $client = new \swoole_client(SWOOLE_SOCK_TCP, SWOOLE_SOCK_ASYNC);
         $method = strtoupper($method);
         $client->on("connect", function ($cli) use($url, $method, $parsed_url, $headers, $params) {
             \ZPHP\Common\AsyncHttpClient::clear($cli);
             $path = isset($parsed_url['path']) ? $parsed_url['path'] : '/';
             if (!empty($params)) {
                 $query = http_build_query($params);
                 if ('GET' == $method) {
                     $path .= "?" . $query;
                 }
             }
             $sendData = $method . " {$path} HTTP/1.1\r\n";
             $headers = array('Host' => $parsed_url['host'], 'Connection' => 'keep-alive', 'Pragma' => 'no-cache', 'Accept' => 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8', 'User-Agent' => 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.80 Safari/537.36', 'Referer' => $url, 'Accept-Encoding' => 'gzip, deflate, sdch', 'Accept-Language' => 'zh-CN,zh;q=0.8') + $headers;
             foreach ($headers as $key => $val) {
                 $sendData .= "{$key}: {$val}\r\n";
             }
             if ('POST' === $method) {
                 $sendData .= "Content-Length: " . strlen($query) . "\r\n";
                 $sendData .= "\r\n" . $query;
             } else {
                 $sendData .= "\r\n";
             }
             $cli->send($sendData);
         });
         $client->on("receive", function ($cli, $data) use($callback) {
             $ret = self::parseBody($cli, $data, $callback);
             if (is_array($ret)) {
                 call_user_func_array($callback, array($cli, $ret));
             }
         });
         $client->on("error", function ($cli) {
             \ZPHP\Common\AsyncHttpClient::clear($cli);
         });
         $client->on("close", function ($cli) {
             \ZPHP\Common\AsyncHttpClient::clear($cli);
         });
         $client->connect($ip, $port);
     });
 }
 public function onReceive($serv, $fd, $from_id, $rdata)
 {
     $client = Client::getInstance($fd);
     $data = $client->cryptor->decrypt($rdata);
     Trace::debug("\n\n\n\n" . str_repeat('#', 20) . "\nquerytimes:" . ++$this->querytimes . "\n" . str_repeat('#', 20) . "\n=======================\nonReceive {$from_id} : {$fd}  lenght:" . strlen($data) . " content:\n=======================\n" . substr($data, 0, 50) . "...\n=======================");
     if (false === $client->hasInit()) {
         $header = Sock5::parseHeader($data);
         if (!$header) {
             return $serv->close($fd);
         }
         $client->init($header['addr']);
         if (strlen($data) > $header['length']) {
             $data = substr($data, $header['length']);
             $client->send($data);
         }
         swoole_async_dns_lookup($header['addr'], function ($host, $ip) use($header, $client, $fd) {
             Trace::debug("dnslookup >{$fd}, {$host}, {$ip} ");
             $client->connect(ip2long($ip), $header['port']);
         });
     } else {
         $client->send($data);
     }
 }
Example #4
0
File: Http.php Project: eyehere/aha
 /**
  * @brief 对外请求开始loop
  */
 public function loop($callback = null)
 {
     if (null !== $callback) {
         $this->setCallback($callback);
     }
     $this->_buildRequest();
     if ($this->_objClient->sock && $this->_objClient->isConnected()) {
         $this->_send($this->_objClient);
         //连接池取出的连接 send失败就关闭了吧
         return parent::loop();
     }
     if (null !== $this->_ip) {
         $this->_objClient->connect($this->_ip, $this->_port, $this->_connectTimeout);
         return parent::loop();
     }
     //异步查找ip
     swoole_async_dns_lookup($this->_host, function ($host, $ip) {
         $this->_ip = $ip;
         $this->_objClient->connect($this->_ip, $this->_port, $this->_connectTimeout);
         return parent::loop();
     });
 }
Example #5
0
 function getResult($response)
 {
     $client = new swoole_redis();
     $ip = "127.0.0.1";
     $port = 6379;
     $client->connect($ip, $port, function (swoole_redis $client, $result) use($response) {
         if ($result === false) {
             echo "connect to redis server failed\n";
             return false;
         }
         $client->GET('test', function (swoole_redis $client, $result) use($response) {
             //echo "get  result is :" . $result;
             $client->close();
             $cityId = '01010101';
             swoole_async_dns_lookup("weather.gtimg.cn", function ($host, $ip) use($cityId, $response) {
                 if (empty($ip)) {
                     return false;
                 } else {
                     $httpcli = new swoole_http_client($ip, 80);
                     //$httpcli->on("close", function($httpcli){});
                     $url = "/qqindex/" . $cityId . ".js?_ref=14";
                     $httpcli->get($url, function ($hcli) use($response) {
                         //echo "get content is" . $hcli->body;
                         $retWeather = iconv("GBK", 'UTF-8', $hcli->body);
                         //echo "ret:" . $retWeather;
                         $hcli->close();
                         $response->header('Content-Type', 'application/json');
                         $response->write(json_encode($retWeather));
                         $response->end();
                     });
                 }
             });
         });
     });
 }
Example #6
0
<?php

for ($i = 0; $i < 100; $i++) {
    swoole_async_dns_lookup("www.baidu{$i}.com", function ($host, $ip) {
        echo "{$host} reslove to {$ip}\n";
    });
}
function dnslookup($domain_name, $callback)
{
    swoole_async_dns_lookup($domain_name, function ($host, $ip) use($callback) {
        $callback($ip);
    });
}
Example #8
0
<?php

swoole_async_dns_lookup("www.baidu.com", function ($host, $ip) {
    echo "{$host} reslove to {$ip}\n";
    swoole_event_exit();
});
Example #9
0
<?php

swoole_async_set(array('dns_lookup_random' => true));
swoole_async_dns_lookup("www.sina.com.cn", function ($host, $ip) {
    echo "{$host} reslove to {$ip}\n";
    swoole_async_dns_lookup("www.sina.com.cn", function ($host, $ip) {
        echo "{$host} reslove to {$ip}\n";
    });
});