Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to bind stream_socket_client to an interface in php?

I have some functions that use stream_socket_client (not curl) in php and I have multiple eth1 eth2 ...etc interfaces with different ips So i want to use different interfaces when i connect as a client, can I do that? I can't find no option for that nor in php.ini

like image 251
PartySoft Avatar asked Feb 21 '26 01:02

PartySoft


1 Answers

Here is how to add IP interface in stream_socket_client

// connect to the internet using the '192.168.0.100' IP
$opts = array(
    'socket' => array(
        'bindto' => '192.168.0.100:0',
    ),
);


// create the context...
$context = stream_context_create($opts);
$fp = @stream_socket_client ( $link, $errno, $errstr, 120, STREAM_CLIENT_CONNECT, $context);

http://pt.php.net/manual/en/context.socket.php

like image 88
user969068 Avatar answered Feb 23 '26 15:02

user969068



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!