How to specifcy a source IP address with fopen

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
emp
Forum Newbie
Posts: 3
Joined: Fri Jan 16, 2009 6:12 pm

How to specifcy a source IP address with fopen

Post 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!
User avatar
it2051229
Forum Contributor
Posts: 312
Joined: Tue Dec 25, 2007 8:34 pm

Re: How to specifcy a source IP address with fopen

Post 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
emp
Forum Newbie
Posts: 3
Joined: Fri Jan 16, 2009 6:12 pm

Re: How to specifcy a source IP address with fopen

Post 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
emp
Forum Newbie
Posts: 3
Joined: Fri Jan 16, 2009 6:12 pm

Re: How to specifcy a source IP address with fopen

Post by emp »

I should have mentioned that I am running this via the command line, not via a web server.
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Re: How to specifcy a source IP address with fopen

Post 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.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: How to specifcy a source IP address with fopen

Post 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.
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
it2051229
Forum Contributor
Posts: 312
Joined: Tue Dec 25, 2007 8:34 pm

Re: How to specifcy a source IP address with fopen

Post 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.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: How to specifcy a source IP address with fopen

Post 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.
There are 10 types of people in this world, those who understand binary and those who don't
Post Reply