Exemple #1
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();
                     });
                 }
             });
         });
     });
 }
Exemple #2
0
<?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;
    });
});