Created
March 19, 2012 01:33
-
-
Save mgng/2089444 to your computer and use it in GitHub Desktop.
t.coなどから本当のURLを取り出す方法 を PHP で
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function unko( $url ){ | |
$def = stream_context_get_default( | |
array( | |
'http' => array( | |
'method' => "HEAD", | |
'protocol_version' => "1.1", | |
'header' => "Accept-Encoding: gzip\r\n". | |
"Connection: close\r\n", | |
) | |
) | |
); | |
$headers = @get_headers( $url ); | |
if ( !$headers ) { return false; } | |
foreach( $headers as $header ) { | |
if ( preg_match('/\ALocation:\s*(.+)/i', $header, $m) === 1 ) { | |
return $m[1]; | |
} | |
} | |
return ''; | |
} | |
var_dump( unko('http://t.co/0xXdwFgd') ); // string(24) "https://internetweek.jp/" | |
var_dump( unko('http://bit.ly/2U7gvY') ); // string(22) "http://www.google.com/" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment