缩短网址浏览器中未显示的“bit.ly”链接

时间:2014-09-20 10:47:01

标签: php codeigniter web bit.ly

我使用以下代码创建了bit.ly链接

 function make_bitly_url($url,$format = 'xml',$version = '2.0.1')
        {

            $login="urlogin";
            $appkey="ur_api_key";   

            $bitly = 'http://api.bit.ly/shorten?version='.$version.'&longUrl='.urlencode($url).'&login='.$login.'&apiKey='.$appkey.'&format='.$format;
                $response = file_get_contents($bitly);

                $xml = simplexml_load_string($response);

            return $response;

        }   

我成功获得了响应,因为缩短了网址,但点击它会在浏览器的url地址栏中显示原始网址

2 个答案:

答案 0 :(得分:1)

正如GolezTrol在评论中所提到的,Bitly链接的目的是提供一个记录点击流量的短网址,并将用户重定向到所需的长网址。比特链接不会永久掩盖他们指向的长URL。

这与重定向发生所需的短时间(通常<200ms)相结合意味着您通常不会在浏览器的位置栏中看到Bitly网址。

答案 1 :(得分:0)

请参阅https://stackoverflow.com/a/41680608/7426396

我实现了获取纯文本文件的每一行,每行有一个缩短的url,相应的重定向url:

<?php
// input: textfile with one bitly shortened url per line
$plain_urls = file_get_contents('in.txt');
$bitly_urls = explode("\r\n", $plain_urls);

// output: where should we write
$w_out = fopen("out.csv", "a+") or die("Unable to open file!");

foreach($bitly_urls as $bitly_url) {
  $c = curl_init($bitly_url);
  curl_setopt($c, CURLOPT_USERAGENT, 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 Safari/537.36');
  curl_setopt($c, CURLOPT_FOLLOWLOCATION, 0);
  curl_setopt($c, CURLOPT_HEADER, 1);
  curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($c, CURLOPT_CONNECTTIMEOUT, 20);
  // curl_setopt($c, CURLOPT_PROXY, 'localhost:9150');
  // curl_setopt($c, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);
  $r = curl_exec($c);

  // get the redirect url:
  $redirect_url = curl_getinfo($c)['redirect_url'];

  // write output as csv
  $out = '"'.$bitly_url.'";"'.$redirect_url.'"'."\n";
  fwrite($w_out, $out);
}
fclose($w_out);

玩得开心,享受! PW