2

Any ideas if bit.ly is doing anything other than simple Location: header to redirect users?

Facebook is able to parse info about final destination when using bit.ly link, but not my project http://guubo.com/aaaaab links that use simple Location: header.

I checked bit.ly headers, they look ordinary.

Gajus
  • 69,002
  • 70
  • 275
  • 438
  • 1
    I suspect that they do a fetch (e.g. using `curl`) and then inspect the headers. – El Yobo May 28 '11 at 13:16
  • make sure you have no html tags. – buhbang May 28 '11 at 13:18
  • I suspect FB is calling the bit.ly API, because it's popular enough to have warranted the effort required: [link](http://code.google.com/p/bitly-api/wiki/ApiDocumentation#/v3/expand) – xs0 May 28 '11 at 13:18
  • Facebook/Bit.ly? Facebook obviously does that, yes and that is the issue: while it works with bit.ly links, it doesn't with guubo.com. I wonder what they are doing different. Bit.ly does not do that, they return empty body. – Gajus May 28 '11 at 13:19
  • @zzlawlzz no content is returned at all. @xs0 what about it? – Gajus May 28 '11 at 13:20
  • Looks like a normal redirect to me; see my answer below. – El Yobo May 28 '11 at 13:21
  • Facebook probably knows bit.ly is a short URL service and follows the *Location* redirection automatically. – Gumbo May 28 '11 at 13:31

2 Answers2

4

I looked into it further. Try the following from the command line

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

You can then look at the contents of headers.txt, which will look like

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

So, no, they're doing a normal 301 redirect. You can do the same inspection in your PHP code using PHP's curl bindings to get the headers to figure out the real site if you want.

El Yobo
  • 14,823
  • 5
  • 60
  • 78
  • `header('HTTP/1.1 301 Moved'); header('Location: ' . $url);` when link is shared on Facebook.com it still shares guubo.com's details rather than the `$url`'s. – Gajus May 28 '11 at 13:24
  • 1
    So, the answer is that *Facebook* is doing something different, not bit.ly. Which is unfortunate from your perspective, if you're trying to write a URL shortener, as it may not be possible to work around. – El Yobo May 28 '11 at 13:25
  • FWIW, you could also use `curl -I` or `curl -D -` to print the response headers to stdout instead of saving them to a file. – Chris Frederick Jul 19 '19 at 06:59
  • Yeah, I have an alias set up to dump them these days - `alias headers='curl -s -D - -o /dev/null'`, then just call `headers somesite.com`. – El Yobo Jul 19 '19 at 11:53
0

see https://stackoverflow.com/a/41680608/7426396

I implemented to get a each line of a plain text file, with one shortened url per line, the according redirect 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);

Have fun and enjoy! pw

Community
  • 1
  • 1
p-w
  • 1