2016-11-24 167 views

回答

0

让所有重定向站点的列表,并检查对他们的网址的域名。

$redirecters = array("goo.gl", "tinyurl.com", ...); 
$domain = parse_url($url, PHP_URL_HOST); 
if (in_array($domain, $redirecters)) { 
    echo "That's a redirect URL!"; 
} 
+0

这可能是一种方式,但链接缩短脚本无处不在。我知道我的一些朋友谁连接缩短私人用途。 – Cunning

+0

不仅goo.gl或tinyurl.com可以做到这一点。 www.example.com也可以做到。 –

+0

@servioretwedijotu我知道,你需要列出所有你知道的列表。没有自动的方法可以说明一个URL会变成一个url缩写。很多URL都会执行重定向,因此无法知道原因是什么。 – Barmar

0

这个代码利用卷曲遵循基于定义的阈值的任意重定向以确定URL缩短:

!defined('THRESHOLD')?define('THRESHOLD',2):null; 
$i = 0; 
$url = 'goo.gl/Vmnf'; 
while ($i<THRESHOLD) 
{ 
    $ch = curl_init(); 

    // set URL and other appropriate options 
    $options = array(CURLOPT_URL => $url, 
        CURLOPT_HEADER => true, 
        CURLOPT_CRLF => true, 
        CURLOPT_FOLLOWLOCATION => false, 
        CURLOPT_FRESH_CONNECT => true, 
        CURLOPT_TCP_NODELAY => true, 
        CURLOPT_RETURNTRANSFER => true, 
        ); 

    curl_setopt_array($ch, $options); 

    // grab URL and pass it to the browser 
    $trans = curl_exec($ch); 

    // close cURL resource, and free up system resources 
    curl_close($ch); 

    // Processing 
    preg_match('/^(?P<header>.*?)(?:\r?\n){2}(?P<body>.*)?$/s', $trans, $ptrans); 
    if(empty($ptrans['header'])) break; 
    $headers = preg_split('/\r?\n/', $ptrans['header']); 
    preg_match('/^HTTP\/(?P<http_varsion>.*?)\s(?P<http_code>.*?)\s(?P<http_description>.*)?$/m', $headers[0], $matches); 
    unset($headers[0]); 
    if ((int)($matches['http_code']/100) != 3) 
    { 
     break; 
    } 
    for($j = 1; $j < count($headers); $j++) 
    { 
     preg_match('/^(?P<name>.*?):\s+(?P<value>.*)$/',$headers[$j], $header); 
     $headers[$header['name']] = $header['value']; 
     unset($headers[$j]); 
    } 
    $http = [ 
     'version' => $matches['http_varsion'], 
     'code' => $matches['http_code'], 
     'description' => $matches['http_description'], 
     'header' => $headers, 
     'body' => $ptrans['body'], 
    ]; 
    if (empty($http['header']['Location'])) 
     break; 
    $url = $http['header']['Location']; 
    $i++; 
} 
echo $i;