PHP で URL のリダイレクト先の URL を取得する方法

短縮 URL などのリダイレクト先を調べるには get_headers() を使うと簡単です。

1
2
$header = get_headers("http://www.example.com/1234567890");
print_r($header);

以下のようにリダイレクト先の URL を取得できます。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
Array
(
    [0] => HTTP/1.0 302 Found
    [1] => Location: http://example.org/test
    [2] => Cache-Control: private
    [3] => Content-Type: text/html; charset=UTF-8
    [4] => Date: Wed, 10 Aug 2011 21:11:38 GMT
    [5] => Server: gws
    [6] => Content-Length: 232
    [7] => X-XSS-Protection: 1; mode=block
    [8] => HTTP/1.1 200 OK
    [9] => Date: Wed, 10 Aug 2011 21:11:38 GMT
    [10] => Server: Apache
    [11] => Set-Cookie: session_id=1234567890----ab; expires=Tue, 19-Jan-2038 03:14:07 GMT; domain=weble.org; path=/;
    [12] => Vary: Host
    [13] => Connection: close
    [14] => Content-Type: text/html
)

コメント

コメントは受け付けていません。