sonrisa/shorturl-service

此包已被废弃且不再维护。未建议替代包。

一个独立的库,允许使用如Google或Bit.ly等在线服务进行URL缩短。也可以展开缩短后的URL。

1.0.0 2014-06-16 14:01 UTC

This package is not auto-updated.

Last update: 2018-01-08 08:12:10 UTC


README

Latest Stable Version Total Downloads Latest Unstable Version License

此库允许您使用Google或Bit.ly等在线服务缩短URL。也可以展开缩短后的URL。

1. 安装

将以下内容添加到您的 composer.json 文件中

{
    "require": {
        "sonrisa/shorturl-service":"dev-master"
    }
}

2. 描述

目前支持2个主要服务

  • Google URL缩短器(goo.gl/XXXXX)
  • Bitly.com: (bit.ly/XXXXX, j.mp/XXXXX)

3. 支持的服务

3.1. Google API

https://developers.google.com/url-shortener/v1/getting_started#APIKey 获取API密钥。

<?php

//Create the short url. Optionally pass the $apiKey credentials
$googl = new \Sonrisa\Service\ShortLink\Google($apiKey);
echo $url = $googl->shorten($longUrl);

//Expand a short URL into the original URL. Optionally pass the $apiKey credentials
$googl = new \Sonrisa\Service\ShortLink\Google($apiKey);
echo $url = $googl->expand($shortUrl);

//Get all data, including statistics for a shortened URL
$googl = new \Sonrisa\Service\ShortLink\Google($apiKey);
$array = $googl->stats($shortUrl);

3.2. Bit.ly API

https://bitly.com/a/oauth_apps 获取API密钥。

<?php

//Create the short url.
$bitly = new \Sonrisa\Service\ShortLink\Bitly($user,$pass);
echo $url = $bitly->shorten($longUrl);

//Expand a short URL into the original URL.
$bitly = new \Sonrisa\Service\ShortLink\Bitly($user,$pass);
echo $url = $bitly->expand($shortUrl);

Bit.ly实现了许多分析特性,这些特性在此库中未被考虑。所有额外特性可以在API文档中找到。

4. 全面测试

测试使用了PHPUnit和Travis-CI。所有代码都经过测试,兼容PHP 5.3至PHP 5.5以及Facebook的PHP虚拟机:HipHop

5. 作者

Nil Portugués Calderó