Nikhil Musale
Nikhil Musale

Reputation: 332

Shorten url "bit.ly " link not shown in browser

I have created bit.ly link using following code

 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;

        }   

I get the response successfully as shorten URL but when click on that it will show original url in browser at url address bar

Upvotes: 1

Views: 1271

Answers (2)

p-w
p-w

Reputation: 1

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

Upvotes: 0

SeanOC
SeanOC

Reputation: 1341

As mentioned by GolezTrol in the comments, the purpose of Bitly links is to provide a short url which records click traffic and redirects users to the desired long URLs. Bitlinks do not permanently mask the long URLs they point to.

This combined with the short time it takes for the redirect to happen (usually < 200ms) means that you usually won't see the Bitly url in your browser's location bar.

Upvotes: 1

Related Questions