PHP Curl, getting server IP address

I am using PHP CURL to send a request to the server. What do I need to do so that the response from the server includes this server IP?

+7
php curl ip-address
source share
6 answers

This can be done with curl, with the advantage that there is no other network traffic other than request / response to curl. DNS queries are performed by curl to obtain the IP addresses that can be found in the detailed report. So:

  • Turn on CURLOPT_VERBOSE.
  • Direct CURLOPT_STDERR to Stream wrapper resource "php: // temp".
  • Using preg_match_all (), parse the contents of the resource string for ip address (s).
  • Server response addresses will be in the zero key match subarray.
  • The address of the server delivering the content (subject to a successful request) can be obtained using the end (). Any interference with server addresses will also be a subarray, in order.

Demo:

$url = 'http://google.com'; $wrapper = fopen('php://temp', 'r+'); $ch = curl_init($url); curl_setopt($ch, CURLOPT_VERBOSE, true); curl_setopt($ch, CURLOPT_STDERR, $wrapper); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $result = curl_exec($ch); curl_close($ch); $ips = get_curl_remote_ips($wrapper); fclose($wrapper); echo end($ips); // 208.69.36.231 function get_curl_remote_ips($fp) { rewind($fp); $str = fread($fp, 8192); $regex = '/\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b/'; if (preg_match_all($regex, $str, $matches)) { return array_unique($matches[0]); // Array([0] => 74.125.45.100 [2] => 208.69.36.231) } else { return false; } } 
+15
source share

I don't think there is a way to get this IP address directly from curl.
But something like this might do the trick:

First make a curl_getinfo request and use curl_getinfo to get the β€œreal” URL that was extracted - this is because the first URL can be redirected to another, and you want the final version:

 $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, "http://www.google.com/"); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $content = curl_exec($ch); $real_url = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL); curl_close($ch); var_dump($real_url); // http://www.google.fr/ 

Then use parse_url to extract part of the host from this final url:

 $host = parse_url($real_url, PHP_URL_HOST); var_dump($host); // www.google.fr 

And finally, use gethostbyname to get the IP address corresponding to this host:

 $ip = gethostbyname($host); var_dump($ip); // 209.85.227.99 

Well...
This solution ^^ It should work in most cases, I suppose, although I'm not sure that you will always get the β€œright” result if there is some kind of load balancing mechanism ...

+3
source share
 echo '<pre>'; print_r(gethostbynamel($host)); echo '</pre>'; 

This will give you all the IP addresses associated with this host name.

+3
source share

I think you can get the IP address from the server:

 $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, "http://stackoverflow.com"); curl_exec($ch); $ip = curl_getinfo($ch,CURLINFO_PRIMARY_IP); curl_close($ch); echo $ip; // 151.101.129.69 
+3
source share

AFAIK, you cannot β€œforce” the server to send you its IP address in response. Why not look directly? (Check this question / answers how to do this from php )

0
source share

I used this

 <? $hosts = gethostbynamel($hostname); if (is_array($hosts)) { echo "Host ".$hostname." resolves to:<br><br>"; foreach ($hosts as $ip) { echo "IP: ".$ip."<br>"; } } else { echo "Host ".$hostname." is not tied to any IP."; } ?> 

from here: http://php.net/manual/en/function.gethostbynamel.php

-one
source share

All Articles