flamer Posted March 4, 2020 Share Posted March 4, 2020 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; Quote Link to comment https://forums.phpfreaks.com/topic/310196-php-bypass-system-host-file/ Share on other sites More sharing options...
gw1500se Posted March 4, 2020 Share Posted March 4, 2020 It is not clear to me what you are really asking. If you just want to use an IP address in the URL, that is not unusual. $url = "http://127.0.0.1/index.php"; Quote Link to comment https://forums.phpfreaks.com/topic/310196-php-bypass-system-host-file/#findComment-1575133 Share on other sites More sharing options...
requinix Posted March 4, 2020 Share Posted March 4, 2020 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. Quote Link to comment https://forums.phpfreaks.com/topic/310196-php-bypass-system-host-file/#findComment-1575134 Share on other sites More sharing options...
flamer Posted March 5, 2020 Author Share Posted March 5, 2020 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!! Quote Link to comment https://forums.phpfreaks.com/topic/310196-php-bypass-system-host-file/#findComment-1575152 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.