Skip to content

Instantly share code, notes, and snippets.

@mgng
Created March 19, 2012 01:33
Show Gist options
  • Save mgng/2089444 to your computer and use it in GitHub Desktop.
Save mgng/2089444 to your computer and use it in GitHub Desktop.
t.coなどから本当のURLを取り出す方法 を PHP で
<?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