PHP 实现“万能”的短网址还原

评论2,046

PHP 实现“万能”的短网址还原

PHP 实现“万能”的短网址还原

/***
 * 万能短网址还原函数
 * @param $shortUrl 短网址
 * @return 原始网址 | 空(还原失败或非短网址)
 */
function restoreUrl($shortUrl) {
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $shortUrl);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:70.0) Gecko/20100101 Firefox/70.0');
    curl_setopt($curl, CURLOPT_HEADER, true);
    curl_setopt($curl, CURLOPT_NOBODY, false);
    curl_setopt($curl, CURLOPT_TIMEOUT, 15);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);
    curl_setopt($curl, CURLOPT_ENCODING, 'gzip');
    $data = curl_exec($curl);
    $curlInfo = curl_getinfo($curl);
    curl_close($curl);
    if($curlInfo['http_code'] == 301 || $curlInfo['http_code'] == 302) {
        return $curlInfo['redirect_url'];
    }
    return '';
}

使用方法:

$shortUrl = 'https://url.cn/54VbB8h';    // 要还原的短网址
$orinalUrl = restoreUrl($shortUrl);
if($orinalUrl) {
    echo "短网址 {$shortUrl} 的还原结果:{$orinalUrl}";
} else {
    echo "短网址还原失败";
}

经过实测,该函数可以顺利实现下列短网址的还原:

  • https://url.cn/54VbB8h
  • http://t.cn/AiR8Qoyp
  • http://uee.me/cAhq8
  • http://rrd.me/eWCg3
  • https://sohu.gg/MSYnnHo02
  • https://dwz.cn/I5l2YWKL
  • http://1t.click/bceu
  • http://dwz.win/qMp
  • http://qq.cn.hn/e8N
  • http://tb.cn.hn/x7X
  • http://jd.cn.hn/aaK9
  • http://tinyurl.com/y5gkl3v9
  • http://u6.gg/sLaav
  • http://c7.gg/fQBWn
  • http://985.so/ejaT
  • http://new.3qtt.cn/1hqafu
  • http://dwz1.cc/DvjLsDcC
  • https://ml.mk/‌‌‌​‌​​‌​‌​‌‌​‌‌‌/
  • https://ml.mk/BvZ
  • http://suo.im/555AiB
  • https://suo.dog/msugd
  • http://sina.lt/eEyd
  • http://mrw.so/4woTLt

 最后更新:2022-6-9

发表评论