gpt4 book ai didi

PHP Curl,检索服务器 IP 地址

转载 作者:可可西里 更新时间:2023-11-01 12:23:27 24 4
gpt4 key购买 nike

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

最佳答案

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

  • 开启 CURLOPT_VERBOSE。
  • 将 CURLOPT_STDERR 指向一个“php://temp”流包装器资源。
  • 使用preg_match_all(),解析资源的 ip 字符串内容地址。
  • 响应服务器地址将在匹配数组的零键中子数组。
  • 发送服务器的地址内容(假设成功请求)可以用结束()。任何干预服务器的地址也将在子数组,按顺序排列。

演示:

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

关于PHP Curl,检索服务器 IP 地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1369760/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com