PHP Curl,检索服务器 IP 地址

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1369760/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 02:13:35  来源:igfitidea点击:

PHP Curl, retrieving Server IP Address

phpcurlip-address

提问by Beier

I'm using PHP CURL to send a request to a server. What do I need to do so the response from server will include that server's IP address?

我正在使用 PHP CURL 向服务器发送请求。我需要做什么才能让服务器的响应包含该服务器的 IP 地址?

回答by GZipp

This canbe done with curl, with the advantage of having no other network traffic besides the curl request/response. DNS requests are made by curl to get the ip addresses, which can be found in the verbose report. So:

可以通过 curl 完成,优点是除了 curl 请求/响应之外没有其他网络流量。DNS 请求由 curl 发出以获取 ip 地址,可以在详细报告中找到。所以:

  • Turn on CURLOPT_VERBOSE.
  • Direct CURLOPT_STDERR to a "php://temp" stream wrapper resource.
  • Using preg_match_all(), parse the resource's string content for the ip address(es).
  • The responding server addresses will be in the match array's zero-key subarray.
  • The address of the server delivering the content (assuming a successful request) can be retrieved with end(). Any intervening servers' addresses will also be in the subarray, in order.
  • 开启 CURLOPT_VERBOSE。
  • 将 CURLOPT_STDERR 定向到“ php://temp”流包装器资源。
  • 使用preg_match_all()解析 ip 地址的资源字符串内容。
  • 响应服务器地址将在匹配数组的零键子数组中。
  • 可以使用end()检索提供内容的服务器地址(假设请求成功 。任何中间服务器的地址也将按顺序出现在子阵列中。

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;
    }
}

回答by mmttato

I think you should be able to get the IP address from the server with:

我认为您应该能够通过以下方式从服务器获取 IP 地址:

$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

回答by Pascal MARTIN

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

我认为没有办法直接从 curl 获取该 IP 地址。
但是这样的事情可以解决问题:

First, do the curl request, and use curl_getinfoto get the "real" URL that has been fetched -- this is because the first URL can redirect to another one, and you want the final one :

首先,执行 curl 请求,并使用它curl_getinfo来获取已获取的“真实”URL——这是因为第一个 URL 可以重定向到另一个 URL,而您想要最后一个:

$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_urlto extract the "host" part from that final URL :

然后,用于parse_url从该最终 URL 中提取“主机”部分:

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

And, finally, use gethostbynameto get the IP address that correspond to that host :

最后,用于gethostbyname获取对应于该主机的 IP 地址:

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

Well...
That's a solution ^^ It should work in most cases, I suppose -- though I'm not sure you would always get the "correct" result if there is some kind of load-balancing mecanism...

嗯......
这是一个解决方案^^它应该在大多数情况下工作,我想 - 尽管我不确定如果有某种负载平衡机制你会总是得到“正确”的结果......

回答by Alix Axel

echo '<pre>';
print_r(gethostbynamel($host));
echo '</pre>';

That will give you all the IP addresses associated with the given host name.

这将为您提供与给定主机名关联的所有 IP 地址。

回答by Henrik Opel

AFAIK you can not 'force' the server to send you his IP address in the response. Why not look it up directly? (Check this question/answers for how to do that from php)

AFAIK 您不能“强制”服务器在响应中向您发送他的 IP 地址。为什么不直接查呢?(检查此问题/答案以了解如何从 php 执行此操作

回答by alekone

I used this one

我用过这个

<?
$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

从这里:http: //php.net/manual/en/function.gethostbynamel.php