Jump to content

PHP bypass system host file


flamer

Recommended Posts

HI all,

this is a strange request so bear with me. I have setup a server on my local machine, what I want to do is scrape info off the real server out on the internet in some of the pages.

I have that working using curl for different domains, but the issue is I have a 127.0.0.1 host entry for the domain in question because the local server name has to match the real server name for the application to run.

I can get the real IP using a shell ie: echo exec('nslookup example.com 1.1.1.1'); but I don't know how to get curl to use that IP or force curl to use a different name server, any ideas?

 

CURL code:

 

thanks in advance!!

 

$url = "http://example.com/index.php";


$fields = [
    'id'      => '1234'
   
];


$fields_string = http_build_query($fields);

$ch = curl_init();

curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, true);
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
curl_setopt($ch,CURLOPT_RETURNTRANSFER, true); 

$result = curl_exec($ch);
echo $result;

 

Link to comment
Share on other sites

You may be able to use CURLOPT_DNS_INTERFACE or CURLOPT_DNS_LOCAL_IP4 to force DNS over your external interface. That would be best. Not sure if it will bypass the system's own resolution though.

If that doesn't do it, leave the hostname in the URL and use CURLOPT_RESOLVE to specify the IP address. For getting the address, PHP doesn't have a built-in way to resolve an address using a custom nameserver, so it'll probably be easiest to use nslookup or dig and run a quick regex on the output to extract the address.

Link to comment
Share on other sites

7 hours ago, requinix said:

You may be able to use CURLOPT_DNS_INTERFACE or CURLOPT_DNS_LOCAL_IP4 to force DNS over your external interface. That would be best. Not sure if it will bypass the system's own resolution though.

If that doesn't do it, leave the hostname in the URL and use CURLOPT_RESOLVE to specify the IP address. For getting the address, PHP doesn't have a built-in way to resolve an address using a custom nameserver, so it'll probably be easiest to use nslookup or dig and run a quick regex on the output to extract the address.

You sir are a genuis, wel the two options unfortunately did not work, but that got me on the right path to find this option which allows us to specify the IP address for the hostname in CURL without doing a DNS lookup:

curl_setopt($ch, CURLOPT_RESOLVE, array( "mydomain.com:443:x.x.x.x"));
 where x.x.x is the real public IP adress of the domain

and you can add some ugly code to ensure it still works if the IP address changes:

//lookup domain against specific nameserver
 $nslookup = shell_exec('nslookup mydomain.com 1.1.1.1'); 
 
 //grab the IP from the output
$shell_output = explode(" ", $nslookup);
$currentIP = $shell_output[10];

//add to curl 
curl_setopt($ch, CURLOPT_RESOLVE, array("mydomain.com:443:".$currentIP));

There may be requirement to change the [10] depending on your operating system.

 

thanks!!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.