How to associate stream_socket_client with interface in php?

I have some functions that use stream_socket_client (not curl) in php, and I have several eth1 eth2 ... etc. interfaces with different ips So I want to use different interfaces when I connect as a client, can I do this? I can not find any option for this or in php.ini

+3
source share
2 answers

AFAIK is impossible. Interfaces are abstracted from PHP; you can just use them, not select them.

Edit: let me rephrase this. If you want to make a system / shell call (for example ifconfig -a) and analyze this output, then bind the socket to this specific address, you might be lucky ... but this is like iffy business.

+1
source

Here's how to add an IP interface to 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

+5
source

Source: https://habr.com/ru/post/1774204/


All Articles