public function run($coroutine) { $urlInfo = parse_url($this->url); $timeout = $this->timeout; if (!isset($urlInfo['port'])) { $urlInfo['port'] = 80; } $cli = new \swoole_http_client($urlInfo['host'], $urlInfo['port']); $cli->set(['timeout' => $timeout, 'keep_alive' => 0]); $cli->on('close', function ($cli) { }); $cli->on('error', function ($cli) use($coroutine) { $coroutine->run(false); }); $cli->execute($this->url, function ($cli) use($coroutine) { $coroutine->run($cli->body); }); return self::retBreak(); }
public function run(Promise &$promise) { $urlInfo = parse_url($this->url); $timeout = $this->timeout; if (!isset($urlInfo['port'])) { $urlInfo['port'] = 80; } $cli = new \swoole_http_client($urlInfo['host'], $urlInfo['port']); $cli->set(array('timeout' => $timeout, 'keepalive' => 0)); $cli->on("error", function ($cli) use(&$promise) { Timer::del($cli->sock); $promise->accept(['http_data' => null, 'http_error' => 'Connect error']); }); $cli->on("close", function ($cli) use(&$promise) { }); $cli->execute($this->url, function ($cli) use(&$promise) { Timer::del($cli->sock); $cli->isDone = true; $promise->accept(['http_data' => $cli->body]); }); $cli->isConnected = false; if (!$cli->errCode) { Timer::add($cli->sock, $this->timeout, function () use($cli, &$promise) { @$cli->close(); if ($cli->isConnected) { $promise->accept(['http_data' => null, 'http_error' => 'Http client read timeout']); } else { $promise->accept(['http_data' => null, 'http_error' => 'Http client connect timeout']); } }); } }
<?php $cli = new swoole_http_client('127.0.0.1', 9501); //post request ////$cli->setData(http_build_query(['a'=>123,'b'=>"哈哈"])); //$cli->setHeaders([ // 'Connection' => "Upgrade", // 'Upgrade' => 'websocket', // 'Sec-WebSocket-Key' => '1ZHQOHNfAXBMEUF2yuh1Sg==', // 'Sec-WebSocket-Extensions' => 'permessage-deflate; client_max_window_bits', //]); $cli->on('message', function ($_cli, $frame) { var_dump($frame); }); $cli->upgrade('/', function ($cli) { echo $cli->body; $cli->push("hello world"); });
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(); }); } }); }); }); }
<?php $cli = new swoole_http_client('127.0.0.1', 80); //post request $cli->setHeaders(['User-Agent' => "swoole"]); $cli->addFile(__DIR__ . '/post.data', 'post'); $cli->addFile(dirname(__DIR__) . '/test.jpg', 'debug'); $cli->post('/dump2.php', array("xxx" => 'abc', 'x2' => 'rango'), function ($cli) { echo $cli->body; $cli->close(); });
<?php ini_set('display_errors', 1); //error_reporting(E_ALL); error_reporting(0); $http = new swoole_http_server("", 9501, SWOOLE_BASE); $http->set([]); $i = 0; $http->on('request', function ($request, swoole_http_response $response) use(&$i) { $route = $request->server['request_uri']; if ($route == '/info') { $response->end(json_encode($request)); return; } $cli = new swoole_http_client('127.0.0.1', 9501); $cli->set(['timeout' => 0.3, 'keep_alive' => 1]); //post request $cli->setData(http_build_query(['a' => 123, 'b' => "哈哈"])); $cli->setHeaders(['User-Agent' => "swoole"]); $cli->on('close', function ($cli) use($response) { // echo "close\n"; }); $cli->on('error', function ($cli) use($response) { $response->end("error"); }); $cli->execute('/info', function ($cli) use($response, &$i) { $cli->setHeaders(['User-Agent' => "swoole"]); //get request $cli->execute('/info', function ($cli) use($response, &$i) { $ret = json_encode($cli->headers) . "\nSERVER RESPONSE: " . $cli->body; $response->end($ret);
<?php $cli = new swoole_http_client('127.0.0.1', 80); //post request //$cli->setData(http_build_query(['a'=>123,'b'=>"哈哈"])); $cli->setHeaders(['User-Agent' => "swoole"]); $cli->execute('/dump.php', function ($cli) { echo $cli->body; });
<?php $cli = new swoole_http_client('127.0.0.1', 80); //post request //$cli->setData(http_build_query(['a'=>123,'b'=>"哈哈"])); $cli->setHeaders(['User-Agent' => "swoole"]); $cli->post('/dump.php', array("test" => 'abc'), function ($cli) { echo $cli->body; $cli->get('/dump.php', function ($cli) { echo $cli->body; }); });
<?php ini_set('display_errors', 1); error_reporting(E_ALL); $http = new swoole_http_server("", 9501, SWOOLE_BASE); $http->set(['worker_num' => 2]); $http->on('request', function ($request, swoole_http_response $response) { $route = $request->server['request_uri']; if ($route == '/info') { $response->end(111); return; } $cli = new swoole_http_client('127.0.0.1', 9501); $cli->set(['timeout' => 0.3, 'keep_alive' => 1]); $cli->on('close', function ($cli) use($response) { // echo "close\n"; }); $cli->on('error', function ($cli) use($response) { echo "error\n"; $response->end("error"); }); $cli->execute('/info', function ($cli) use($response) { $cli->execute('/info', function ($cli) use($response) { $ret = json_encode($cli->headers) . $cli->body; $response->end($ret); $cli->close(); //echo "----->Mem: ", memory_get_usage(), "b\n"; }); }); }); $http->start();
<?php $cli = new swoole_http_client('127.0.0.1', 9501); //post request //$cli->setData(http_build_query(['a'=>123,'b'=>"哈哈"])); $cli->setHeaders(['Connection' => "Upgrade", 'Upgrade' => 'websocket', 'Sec-WebSocket-Key' => '1ZHQOHNfAXBMEUF2yuh1Sg==', 'Sec-WebSocket-Extensions' => 'permessage-deflate; client_max_window_bits']); $cli->on('message', function ($_cli, $frame) { var_dump($frame); }); $cli->execute('/', function ($cli) { echo $cli->body; $cli->push("hello world"); });