tzfrs/longurl

此库扩展了短URL到长URL

0.0.5 2015-09-09 14:20 UTC

This package is auto-updated.

Last update: 2024-09-13 02:18:32 UTC


README

此库可用于扩展如 https://#/XdXRudPXH5 这样的短URL,并获取短URL背后的真实URL

安装

通过 composer 安装

{
    "require": {
        "tzfrs/longurl": "0.0.4"
    }
}

运行 composer installcomposer update

注意

当前此库仅支持获取服务、检查URL是否为短URL以及扩展URL的方法,因为我只在一个只需要这些功能的项中使用它。如果您需要额外功能,请直接提交问题。

对于缓存,默认情况下,此库使用 /tmp/ 目录。如果您想更改它,只需在构造函数的第二个参数中定义缓存路径即可

入门

注意:您还可以查看 examples.php 以获取更多示例。

基本解析

<?php
require __DIR__ . '/vendor/autoload.php';

$expand = new \tzfrs\LongURL\Endpoints\Expand();
try {
    print $expand->expandURL('https://#/XdXRudPXH5'); //https://blog.twitter.com/2013/rich-photo-experience-now-in-embedded-tweets-3
    print $expand->expandURL('https://blog.twitter.com/2013/rich-photo-experience-now-in-embedded-tweets-3'); //https://blog.twitter.com/2013/rich-photo-experience-now-in-embedded-tweets-3
} catch (\tzfrs\LongURL\Exceptions\ExpandException $e) {
    print $e->getMessage();
}

列出所有服务

<?php
require __DIR__ . '/vendor/autoload.php';

$services = new \tzfrs\LongURL\Endpoints\Services();
try {
    $services = $services->getServices(); // Object array with all services
} catch (\tzfrs\LongURL\Exceptions\ServicesException $e) {
    print $e->getMessage();
}

检查是否为短URL

<?php
require __DIR__ . '/vendor/autoload.php';

$services = new \tzfrs\LongURL\Endpoints\Services();
try {
    $services->isShortURL('https://#/XdXRudPXH5'); // True
    $services->isShortURL('https://blog.twitter.com/2013/rich-photo-experience-now-in-embedded-tweets-3'); // False
} catch (\tzfrs\LongURL\Exceptions\ServicesException $e) {
    print $e->getMessage();
}