我已经找到bit.ly应用程序接口在php短链接,但我需要做一个循环,在那里将缩短一个链接数组…
举个例子,我有一个数组:
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');但你能告诉我,如何做空这个数组吗?谢谢!
发布于 2013-07-22 17:44:09
<?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-22 17:46:41
我认为唯一可能做到这一点的方法是使用foreach:
$bitly = new bitly('username', 'apikey');
$shortLinks = array();
foreach($longLinks as $longLink) {
$shortLinks [] = $bitly->shorten($longLink);
}$longLinks表示第一个数组,$shortLinks表示短链接(API的结果)。
https://stackoverflow.com/questions/17784243
复制相似问题