http-client
http-client copied to clipboard
How to use multiple local IPs to bind?
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?
The latest version supports that, too. You can use a custom connector for that.
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.
PR to add the example welcome. :-)