Specify source ip using fsockopen

On a server with multiple IPs routed to it, I'd like to use PHP's fsockopen to open from a non-primary-interface ip (or a comparable method to be able to make fread and fwrites from a different ip)

Upvotes: 5

Views: 5193

Answers (2)

netcoder
netcoder

Reputation: 67695

This is not possible with fsockopen. You have to use the sockets wrapper:

$sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_bind($sock, '192.168.1.100');
socket_connect($sock, 'stackoverflow.com', 80);

Upvotes: 7

payne
payne

Reputation: 14177

With the standard arguments offered, it may not be possible.

This article (see: http://bytes.com/topic/php/answers/568317-specify-source-address-interface-use-when-using-fsockopen) suggests that you have to go down a level and use socket_bind().

Upvotes: 2

Related Questions