-1

Rerouting my PHP queries (CURL, fsockopen, file_get_contents) source.com to x.x.x.x following way:

Win OS:

c:\Windows\System32\drivers\etc\hosts
x.x.x.x source.com

Linux OS:

$ sudo gedit /etc/hosts
x.x.x.x source.com

Is it possible to do this at the level of Apache or PHP configuration (i.e., without interference in hosts file)?

Mureinik
  • 297,002
  • 52
  • 306
  • 350
Epexa
  • 265
  • 2
  • 8
  • I'm not clear what you want here. Where is the request being made from, and where do you need it to go to? – IMSoP Apr 11 '15 at 00:55
  • Request comes with a PHP-script on the domain1.com you need to go to a domain2.com (without interfering with the PHP-script itself) – Epexa Apr 11 '15 at 00:59
  • Sorry, that isn't any clearer. Perhaps you could edit the question to include an example of the actual code you're trying to write? Remember that we know nothing about your code except what you tell us, however obvious it is to you after staring at the problem for hours. – IMSoP Apr 11 '15 at 01:01

1 Answers1

1

So you want to force the DNS resolution of domain1.com to another IP (say, domain2.com's IP) but do not want to do this using /etc/hosts and would like to do it in the Apache or PHP configuration only.

Well here the problem is that PHP uses your server's DNS settings set in /etc/resolv.conf and /etc/hosts as an override, and Apache is just a web-server so it cannot change your application's logic or control it's networking.

What would be cleaner here is to save the IP of domain2.com in a php config file and call that IP with curl instead of domain1.com, and you can still add a Host: domain1.com header to your HTTP call if needed.

Edit : Here is an example using curl in PHP.

Let's say

  • domain1.com's IP is 1.1.1.1
  • domain2.com's IP is 2.2.2.2

If your goal is to call 2.2.2.2 but with the domain domain1.com, you just need to change the Host header of your HTTP request, like this :

<?php
$ch = curl_init('http://2.2.2.2/');
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Host: domain1.com'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
echo curl_exec($ch);
Tristan Foureur
  • 1,647
  • 10
  • 23
  • Yes, exactly! Explain an example please: "What would be cleaner here is to save the IP of domain2.com in a php config file and call that IP with curl instead of domain1.com, and you can still add a Host: domain1.com header to your HTTP call if needed." – Epexa Apr 11 '15 at 08:04
  • I just added a PHP example. – Tristan Foureur Apr 11 '15 at 10:57