Phillip Barrett
Phillip Barrett

Reputation: 21

Trying to use bit.ly api to bulk shorten urls

I'm using the bit.ly api to try to shorten multiple urls at once, but not having any success.

    <?php

 $sites = array( 
'http://api.bit.ly/shorten?version=2.0.1&login=bitlyexample&apiKey=R_8b726077f3d5c6029700c29e529395d3&format=text&longUrl=http://myurl.com', 
'http://api.bit.ly/shorten?version=2.0.1&login=bitlyexample&apiKey=R_8b726077f3d5c6029700c29e529395d3&format=text&longUrl=http://myurl3.com', 
); 

foreach ( $sites as $site ) { 
    $xmlDoc = new DOMDocument();  
    if ( @$xmlDoc->load($site) ) { 
        echo "{$site} successful<br />\n"; 
    } else { 
        echo "{$site} invalid<br />\n"; 
    } 
} 
die();  

?>

How do I an array of long urls converted into a list of bit.ly?

Upvotes: 2

Views: 1695

Answers (5)

Joe Watkins
Joe Watkins

Reputation: 17158

You could also use Threads, or a few Workers ...

https://github.com/krakjoe/pthreads

Enjoy ...

Upvotes: 0

Chris Ismael
Chris Ismael

Reputation: 610

Here's a free PHP client library that integrates with the bit.ly API: https://www.mashape.com/mashaper/bitly

Just hook getShortenedUrl into your foreach loop and you should be golden.


Full disclosure, I'm Mashape's Craftsman Advocate and a fellow PHP hacker since I was in college. Feel free to hit me up with any further questions you have.

Upvotes: 1

Lawrence Cherone
Lawrence Cherone

Reputation: 46602

Personally I would use curl over fgc as its faster and as you want to do bulk converts then why not implement curl multi, this will speed up the waiting time for the api request. Faster is better right?

function curl_multi($urls) {
    $curly = array();
    $result = array();

    $mh = curl_multi_init();
    foreach ($urls as $id=>$url) {
        $curly[$id] = curl_init();

        curl_setopt($curly[$id], CURLOPT_URL,            $url);
        curl_setopt($curly[$id], CURLOPT_HEADER,         0);
        curl_setopt($curly[$id], CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curly[$id], CURLOPT_TIMEOUT,        30);
        curl_setopt($curly[$id], CURLOPT_USERAGENT,      'Mozilla/5.0 (Windows NT 5.1; rv:5.0) Gecko/20100101 Firefox/5.0 Firefox/5.0');
        curl_setopt($curly[$id], CURLOPT_REFERER,        $url);
        curl_setopt($curly[$id], CURLOPT_ENCODING,       'gzip,deflate');
        curl_setopt($curly[$id], CURLOPT_AUTOREFERER,    true);
        curl_setopt($curly[$id], CURLOPT_RETURNTRANSFER, true);

        curl_multi_add_handle($mh, $curly[$id]);
    }
    $running = null;
    do {
        curl_multi_exec($mh, $running);
    } while($running > 0);
    foreach($curly as $id => $c) {
        $result[$id] = curl_multi_getcontent($c);
        curl_multi_remove_handle($mh, $c);
    }
    curl_multi_close($mh);
    return $result;
}


$sites = array( 
'http://api.bit.ly/shorten?version=2.0.1&login=bitlyexample&apiKey=R_8b726077f3d5c6029700c29e529395d3&format=text&longUrl=http://myurl.com', 
'http://api.bit.ly/shorten?version=2.0.1&login=bitlyexample&apiKey=R_8b726077f3d5c6029700c29e529395d3&format=text&longUrl=http://myurl3.com', 
); 

$result = curl_multi($sites);

print_r($result);
/*
Array
(
    [0] => http://bit.ly/UhaW9E
    [1] => http://bit.ly/RlX21s
)
*/ 

Upvotes: 0

Jonathan Christensen
Jonathan Christensen

Reputation: 103

Here is a function to generate the links, be sure to replace the login name and api key with yours:

function get_short_link($url) {
  $bitly_login="**login**";
  $bitly_apikey="**apikey**";

  $api_call = file_get_contents("http://api.bit.ly/shorten?version=2.0.1&longUrl=".$url."&login=".$bitly_login."&apiKey=".$bitly_apikey);

  $bitlyinfo=json_decode(utf8_encode($api_call),true);

  if ($bitlyinfo['errorCode']==0) {
    return $bitlyinfo['results'][urldecode($url)]['shortUrl'];
  } else {
    return false;
  }
}

Then, to run the function throw it into a foreach loop:

$bitlyarray = array();
$arraylist = array(
  'http://www.siteone.com',
  'http://www.sitetwo.com',
  'http://www.sitethree.com'
);

foreach($arraylist as $link)
{
  $bitlyarray[] = get_short_link($link);
}

var_dump($bitlyarray);

Hope that helps.

Upvotes: 1

Kris Chant
Kris Chant

Reputation: 289

Try using file_get_contents()

$sites = array(
    'http://api.bit.ly/shorten?version=2.0.1&login=bitlyexample&apiKey=R_8b726077f3d5c6029700c29e529395d3&format=text&longUrl=http://myurl.com',
    'http://api.bit.ly/shorten?version=2.0.1&login=bitlyexample&apiKey=R_8b726077f3d5c6029700c29e529395d3&format=text&longUrl=http://myurl3.com',
);

foreach ( $sites as $site ) {
    $shortened_url = file_get_contents($site);
    if($shortened_url)
        echo "$shortened_url <br/>";
}
die();

Upvotes: 2

Related Questions