如果我有:
domainA.com/out.php :
<?php
header('location: http://domainB.com/');
?>
是否有可能得到url:domainB.com
并且它是IP Address
的,来自domainC.com
domanA.com/out.php
?
我想要什么 :
domainA.com/index.php
<?php
$data = getUrlandIp("domainA.com/out.php");
echo $data[0]; # wanted output (URL) : domainB.com
echo $data[1]; # wanted output (IP) : 133.133.133.133
?>
如果您需要获取所有重定向,您可以这样做
function getRedirectsToUri($uri)
{
$redirects = array();
$http = stream_context_create();
stream_context_set_params(
$http,
array(
"notification" => function() use (&$redirects)
{
if (func_get_arg(0) === STREAM_NOTIFY_REDIRECTED) {
$redirects[] = func_get_arg(2);
}
}
)
);
file_get_contents($uri, false, $http);
return $redirects;
}
这将返回一个包含所有重定向的数组,最后一个条目是最终目的地。
示例(演示)
print_r(getRedirectsToUri('http://bit.ly/VDcn'));
输出
Array (
[0] => http://example.com/
[1] => http://www.iana.org/domains/example/
)
不过,您必须手动查找IP(在此处查看其他答案),但请注意,重定向目标不必是主机名。它也可以是一个IP。
使用 get_headers() 从 URL 获取 http 标头。
像这样的东西应该可以获取域名。
$headers = get_headers('http://domaina.com/out.php', 1);
echo $headers['Location'];
要解析 IP 地址,请查看 gethostbyname() 函数。
echo gethostbyname($headers['Location']);
我不确定你的意思
从 domainC.com?
但是在PHP中,您可以调用gethostbyname('domainB.com')
它将为您提供来自domainA.com/out.php
的 domainB.com 的IP