bit.ly重定向方法


bit.ly redirect method

如果bit.ly正在做除了简单的Location:标头之外的任何事情来重定向用户,有什么想法吗?

当使用bit.ly链接时,Facebook能够解析有关最终目的地的信息,但我的项目http://guubo.com/aaaaab链接使用简单的Location:标题时却无法解析。

我检查了bit.ly头,它们看起来很普通。

我进一步研究了它。从命令行尝试以下操作

curl -D headers.txt http://bit.ly/4m1AUx

然后您可以查看headers.txt的内容,它看起来像

HTTP/1.1 301 Moved
Server: nginx
Date: Sat, 28 May 2011 13:18:21 GMT
Content-Type: text/html; charset=utf-8
Connection: keep-alive
Set-Cookie: _bit=4de0f61d-001f7-008b9-d8ac8fa8;domain=.bit.ly;expires=Thu Nov 24 08:18:21 2011;path=/; HttpOnly
Cache-control: private; max-age=90
Location: http://slashdot.org/
MIME-Version: 1.0
Content-Length: 112

所以,不,他们正在做一个正常的301重定向。如果需要的话,您可以使用PHP的curl绑定在PHP代码中进行同样的检查,以获取头部来确定真实的站点。

请参阅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