PHP code example of moln / swoole-http-client

1. Go to this page and download the library: Download moln/swoole-http-client library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

moln / swoole-http-client example snippets


use Moln\SwooleHttpClient\Client;
$cli = new Client('192.168.11.5');
$cli->get('/test/1.php', function (\Psr\Http\Message\ResponseInterface $response) {
    var_dump((string)$response->getBody());
});

use Moln\SwooleHttpClient\ClientStatic;
use Psr\Http\Message\ResponseInterface;

$cli = ClientStatic::get('http://192.168.11.5/test/1.php', function (ResponseInterface $response) {
 var_dump((string)$response->getBody());
})->on('connect', function () {
  var_dump('connect');
});

$cli = ClientStatic::post('http://192.168.11.5/test/1.php', ['key' => 'val'], function (ResponseInterface $response) {
 var_dump((string)$response->getBody());
});


$client = new \Moln\SwooleHttpClient\ClientChain('192.168.11.5');
$client->on('connect', function ($client) {
    var_dump(spl_object_hash($client));
});

//New 一个新的 Client
$client->get('/test/1.php?s=2&id=3', function ($response) use ($client) {
    //复用 client 对象
    var_dump(spl_object_hash($client->get('/test/1.php?s=1&id=5')));;
    var_dump(spl_object_hash($client->get('/test/1.php?s=1&id=6')));;
});

//没有空闲Client 对象,再 new 一个 Client
$client->get('/test/1.php?s=1&id=4',  function ($response) use ($client) {
});

use Moln\SwooleHttpClient\Client;
$cli = new Client('192.168.11.5');
$cli->setHeaders(['Connection' => 'close']);
$cli->on('connect', function () use ($cli) {
    var_dump('connect event1', $cli->isConnected());
});
$cli->on('connect', function () use ($cli) {
    var_dump('connect event2', $cli->isConnected());
});
$cli->on('error', function () use ($cli) {
    var_dump('connect event2', $cli->isConnected());
});

use Moln\SwooleHttpClient\Client;
$cli = new Client('192.168.11.5');
$cli->get('/test/1.php', function (\Psr\Http\Message\ResponseInterface $response) {
    var_dump((string)$response->getBody());
});
$cli->post('/test/1.php', ['a' => 1], function (\Psr\Http\Message\ResponseInterface $response) {
    var_dump((string)$response->getBody());
});

use Moln\SwooleHttpClient\Client;
$cli = new Client('192.168.11.5');
$cli->send(new \GuzzleHttp\Psr7\Request('POST', '/test/1.php?s=1&id=2', ['X-xxx' => '123'], 'x=1'), 
    function ($response) {
        var_dump((string)$response->getBody());
    }
);

use Moln\SwooleHttpClient\Client;
$cli = new Client('192.168.11.5');
$cli->setTimeout(4);
$cli->setHeaders(['Connection' => 'close']);
$cli->on('connect', function () use ($cli) {
    var_dump('connect', $cli->isConnected());
});
$cli->on('error', function ($cli, $msg) {
    //$cli->isConnected() 判断是连接超时,还是连接上了响应超时
    var_dump('connect error', $msg, $cli->isConnected()); // $msg= 'timeout' ; $msg = 'error'; 
});