2013-07-22 34 views
1

我發現bit.ly的API在PHP中短鏈接,但我需要一個循環,在那裏將被縮短的鏈接陣列...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/... 
) 

我已經包含微小的API(HERE)和usnig PHP代碼,我可以短一個鏈接

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

但你能告訴我,如何縮短這個數組嗎?謝謝!

+0

過度用和RP是正確的,沒有縮短許多API,所以你需要循環你的長URL。 –

回答

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

我看可能這樣做唯一的辦法就是使用的foreach:(API的結果)

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

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

$ longLinks代表第一陣列和$ shortLinks代表的短鏈接