短縮URLのデコード
10月 13th, 2009
bit.lyなどの短縮URLサービスでURLをデコードする際はAPI叩かないでリクエストしてみてレスポンスヘッダ見たほうが楽。
PHP
if (preg_match('|http://bit\.ly/[a-zA-Z0-9]+|', $message, $matches)) {
$request = new HTTP_Request($matches[0], array('allowRedirects' => false));
if (!PEAR::isError($request->sendRequest())) {
if ($request->getResponseCode() == 301) {
$message = str_replace($matches[0], $request->getResponseHeader('location'), $message);
}
}
}