简体   繁体   中英

php - short array of links using bit.ly api

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!

<?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);

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.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM