http-client icon indicating copy to clipboard operation
http-client copied to clipboard

How to use multiple local IPs to bind?

Open xpader opened this issue 5 years ago • 3 comments

I have multiple local ip and interface on server, and need to use these ip to request target server. I found that old version support OP_BINDTO option, but looks like there is no way to do with latest version?

xpader avatar Jan 07 '21 02:01 xpader

The latest version supports that, too. You can use a custom connector for that.

kelunik avatar Jan 21 '21 19:01 kelunik

Oh yes, I found ConnectContext to do that.

class SomeClass 
{

    public function http()
    {
    	$p0 = $this->makePool('192.168.4.58');
    	$p1 = $this->makePool('192.168.4.66');

    	$r0 = yield $p0->request(new Request('http://192.168.4.3/ip.php'));
    	$r1 = yield $p1->request(new Request('http://192.168.4.3/ip.php'));

    	$res = [
    		yield $r0->getBody()->buffer(),
		    yield $r1->getBody()->buffer()
	    ];

    	return print_r($res, true);
    }

    private function makePool($bindTo)
    {
	    $connectContext = (new ConnectContext())->withBindTo($bindTo);

	    $pool = new UnlimitedConnectionPool(new DefaultConnectionFactory(null, $connectContext));

	    return (new HttpClientBuilder())
		    ->usingPool($pool)
		    ->build();
    }

}

http() output:

Array(
   [0] => 192.168.4.58
   [1] => 192.168.4.66
)

Hope theres some examples to show how to do that.

xpader avatar Jan 22 '21 13:01 xpader

PR to add the example welcome. :-)

kelunik avatar Jan 22 '21 15:01 kelunik