Question

I have found bit.ly api to short links in php, but I need to make a loop, where will be shortened a array of links...

So, for example, I have array:

Array
(
    [0] => http://longlink.com/1.php
    [1] => http://longlink.com/2.php
    [2] => http://longlink.com/3.php
    [3] => http://longlink.com/4.php
    [4] => http://longlink.com/5.php
)

and I need to short it to new array like this:

Array
(
    [0] => http://bit.ly/...
    [1] => http://bit.ly/...
    [2] => http://bit.ly/...
    [3] => http://bit.ly/...
    [4] => http://bit.ly/...
)

I have included bitty api (HERE) and usnig php code, I can short one link

$bitly = new bitly('username', 'apikey');
echo $bitly->shorten('http://longlink.com/1.php');

But can you tell me, how to short that array? Thanks!

Was it helpful?

Solution

<?php
$urls = array (
    'http://longlink.com/1.php',
    'http://longlink.com/2.php',
    'http://longlink.com/3.php',
    'http://longlink.com/4.php',
    'http://longlink.com/5.php',
);

$result = array();
$bitly = new bitly('username', 'apikey');
foreach ($urls as $url)
{
    $result[] = $bitly->shorten($url);
}
print_r($result);

OTHER TIPS

Only way I see possible to do this is by using foreach:

$bitly = new bitly('username', 'apikey');

$shortLinks = array();
foreach($longLinks as $longLink) {
    $shortLinks [] = $bitly->shorten($longLink);
}

$longLinks represent first array and $shortLinks represent short links (outcomes of API.)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top