1

我在 php 中找到了用于短链接的 bit.ly api,但我需要创建一个循环,其中将缩短一系列链接......

因此,例如,我有数组:

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
)

我需要将它缩短为这样的新数组:

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

我已经包含了 bitty api ( HERE ) 和 usnig php 代码,我可以缩短一个链接

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

但是你能告诉我,如何缩短那个数组吗?谢谢!

4

2 回答 2

1
<?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);
于 2013-07-22T09:44:09.013 回答
0

我认为可以做到这一点的唯一方法是使用 foreach:

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

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

$longLinks 代表第一个数组,$shortLinks 代表短链接(API 的结果)。

于 2013-07-22T09:46:40.593 回答