首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >bit.ly重定向方法

bit.ly重定向方法
EN

Stack Overflow用户
提问于 2011-05-28 21:14:27
回答 2查看 1.5K关注 0票数 2

如果bit.ly做的不是简单的Location:头来重定向用户,你有什么想法吗?

Facebook能够在使用bit.ly链接时解析有关最终目的地的信息,但不能使用简单的Location:头来解析我的项目http://guubo.com/aaaaab链接。

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

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-05-28 21:20:30

我进一步调查了一下。从命令行尝试执行以下操作

代码语言:javascript
复制
curl -D headers.txt http://bit.ly/4m1AUx

然后,您可以查看headers.txt的内容,如下所示

代码语言:javascript
复制
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代码中使用PHP's curl bindings进行相同的检查,以获取标头来确定实际站点。

票数 4
EN

Stack Overflow用户

发布于 2017-01-17 00:32:46

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

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

代码语言:javascript
复制
<?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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6161852

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档