【问题标题】:php - short array of links using bit.ly apiphp - 使用 bit.ly api 的短链接数组
【发布时间】:2013-07-22 09:39:54
【问题描述】:

我在 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');

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

【问题讨论】:

  • srain 和 RP 是对的,没有缩短许多 API,因此您需要循环遍历长 URL。

标签: php arrays hyperlink short bit.ly


【解决方案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);

【讨论】:

    【解决方案2】:

    我认为唯一可行的方法是使用 foreach:

    $bitly = new bitly('username', 'apikey');
    
    $shortLinks = array();
    foreach($longLinks as $longLink) {
        $shortLinks [] = $bitly->shorten($longLink);
    }
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-20
      • 2014-09-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多