Page 1 of 1

How to specifcy a source IP address with fopen

Posted: Fri Jan 16, 2009 6:15 pm
by emp
Hello,

I can't figure out how to specify a source IP address to use when using fopen. My box has multiple IP addresses on it and I need to specify different sources for different scripts. Any ideas?

Code: Select all

<?php
  $file = fopen('http://www.cnn.com', 'r');
  $contents = stream_get_contents($file);  
  print($contents);
  fclose($file);
?>
 
Thanks!

Re: How to specifcy a source IP address with fopen

Posted: Fri Jan 16, 2009 6:21 pm
by it2051229
you mean something like

Code: Select all

 
$file = fopen('http://127.0.0.1', 'r');
 
are you stealing articles from CNN and posting it in your website? :D

Re: How to specifcy a source IP address with fopen

Posted: Fri Jan 16, 2009 6:36 pm
by emp
The server itself has multiple IP addresses. When fopen runs, it sources the IP connection from the first binded IP. I need to be able to control which IP is used in this application. :D

Re: How to specifcy a source IP address with fopen

Posted: Fri Jan 16, 2009 11:10 pm
by emp
I should have mentioned that I am running this via the command line, not via a web server.

Re: How to specifcy a source IP address with fopen

Posted: Fri Jan 16, 2009 11:17 pm
by Burrito
I'm nearly certain this can't be done with PHP itself. You might be able to do it with Apache but since you're making your request from the CLI, I think you'll have to do it with a *nix network setting somewhere.

Re: How to specifcy a source IP address with fopen

Posted: Sat Jan 17, 2009 4:18 am
by VladSun
You can't do this with fopen() but you can do this with CURL:

http://bg2.php.net/manual/bg/function.curl-setopt.php
CURLOPT_INTERFACE

The name of the outgoing network interface to use. This can be an interface name, an IP address or a host name.

Re: How to specifcy a source IP address with fopen

Posted: Sat Jan 17, 2009 6:37 pm
by it2051229
or you can do it the hard way, you need to get all the IP addresses they are using and fOpen them all one by one.

Re: How to specifcy a source IP address with fopen

Posted: Sun Jan 18, 2009 3:56 am
by VladSun
it2051229 wrote:or you can do it the hard way, you need to get all the IP addresses they are using and fOpen them all one by one.
emp wants to change the source IP address, not the destination one.