コード例 #1
0
 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']);
             }
         });
     }
 }
コード例 #2
0
 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();
 }
コード例 #3
0
<?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);