4

私が持っている場合:

domainA.com/out.php :

<?php
  header('location: http://domainB.com/');
?>

fromdomainB.comでurl : を取得することは可能IP Addressですか?domanA.com/out.phpdomainC.com

私が欲しいもの:

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

3 に答える 3

5

すべてのリダイレクトを取得する必要がある場合は、次のことができます

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でもありえます。

于 2012-06-15T16:54:22.510 に答える
3

get_headers()を使用して、URL から http ヘッダーを取得します。

ドメイン名を取得するには、このようなものが機能するはずです。

$headers = get_headers('http://domaina.com/out.php', 1);
echo $headers['Location'];

IP アドレスを解決するには、gethostbyname()関数を調べます。

echo gethostbyname($headers['Location']);
于 2012-06-15T16:45:41.783 に答える
2

私はあなたが何を意味するのか分かりません

domainC.comから?

ただし、PHP ではgethostbyname('domainB.com')、domainB.com の IP を取得するために呼び出すことができます。domainA.com/out.php

于 2012-06-15T16:48:48.447 に答える