├── config └── tly.php ├── src ├── Facades │ └── TLYApi.php ├── TLYServiceProvider.php └── TLYApiService.php ├── composer.json └── README.md /config/tly.php: -------------------------------------------------------------------------------- 1 | env('TLY_API_TOKEN', ''), 5 | ]; -------------------------------------------------------------------------------- /src/Facades/TLYApi.php: -------------------------------------------------------------------------------- 1 | publishes([ 12 | __DIR__.'/../config/tly.php' => config_path('tly.php'), 13 | ], 'config'); 14 | } 15 | 16 | public function register() 17 | { 18 | $this->mergeConfigFrom(__DIR__.'/../config/tly.php', 'tly'); 19 | 20 | $this->app->singleton('tlyapi', function ($app) { 21 | return new TLYApiService(config('tly.api_token')); 22 | }); 23 | } 24 | } -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "tly/laravel-url-shortener-api", 3 | "description": "A Laravel package for the T.LY URL Shortener API", 4 | "type": "library", 5 | "keywords": [ 6 | "laravel", 7 | "url shortener", 8 | "link shortener", 9 | "url shortener api", 10 | "link shortener api", 11 | "t.ly" 12 | ], 13 | "license": "MIT", 14 | "authors": [ 15 | { 16 | "name": "Tim Leland", 17 | "email": "tim@t.ly" 18 | } 19 | ], 20 | "require": { 21 | "php": "^8.0", 22 | "guzzlehttp/guzzle": "^7.0" 23 | }, 24 | "autoload": { 25 | "psr-4": { 26 | "TLY\\LaravelUrlShortener\\": "src/" 27 | } 28 | }, 29 | "extra": { 30 | "laravel": { 31 | "providers": [ 32 | "TLY\\LaravelUrlShortener\\TLYServiceProvider" 33 | ], 34 | "aliases": { 35 | "TLYApi": "TLY\\LaravelUrlShortener\\Facades\\TLYApi" 36 | } 37 | } 38 | }, 39 | "minimum-stability": "dev", 40 | "prefer-stable": true 41 | } 42 | -------------------------------------------------------------------------------- /src/TLYApiService.php: -------------------------------------------------------------------------------- 1 | apiToken = $apiToken; 15 | } 16 | 17 | private function headers() 18 | { 19 | return [ 20 | 'Authorization' => 'Bearer '.$this->apiToken, 21 | 'Content-Type' => 'application/json', 22 | 'Accept' => 'application/json', 23 | ]; 24 | } 25 | 26 | public function create(array $data) 27 | { 28 | return Http::withHeaders($this->headers()) 29 | ->post("{$this->apiUrl}/shorten", $data) 30 | ->json(); 31 | } 32 | 33 | public function get(string $shortUrl) 34 | { 35 | return Http::withHeaders($this->headers()) 36 | ->get($this->apiUrl, ['short_url' => $shortUrl]) 37 | ->json(); 38 | } 39 | 40 | public function update(array $data) 41 | { 42 | return Http::withHeaders($this->headers()) 43 | ->put($this->apiUrl, $data) 44 | ->json(); 45 | } 46 | 47 | public function delete(string $shortUrl) 48 | { 49 | return Http::withHeaders($this->headers()) 50 | ->delete($this->apiUrl, ['short_url' => $shortUrl]) 51 | ->json(); 52 | } 53 | 54 | public function list(array $params = []) 55 | { 56 | return Http::withHeaders($this->headers()) 57 | ->get("{$this->apiUrl}/list", $params) 58 | ->json(); 59 | } 60 | 61 | public function stats(string $shortUrl) 62 | { 63 | return Http::withHeaders($this->headers()) 64 | ->get("{$this->apiUrl}/stats", ['short_url' => $shortUrl]) 65 | ->json(); 66 | } 67 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # T.LY Laravel URL Shortener API 2 | 3 | This package provides a convenient Laravel wrapper for the [T.LY URL Shortener](https://t.ly/) API. 4 | 5 | T.LY URL Shortener API documentation to create URLs to track, brand, and share short links. This package aims to help you to work with our API. Please see our [API Docs](https://t.ly/docs) if you have any issues. 6 | 7 | ## Create an API Key 8 | 9 | 1. Register a [T.LY Account](https://t.ly/register) 10 | 2. Create an [API Token](https://t.ly/settings#/api) 11 | 12 | ## Installation 13 | 14 | Install via Composer: 15 | 16 | ```bash 17 | composer require tly/laravel-url-shortener-api 18 | ``` 19 | 20 | Publish the configuration: 21 | 22 | ```bash 23 | php artisan vendor:publish --provider="TLY\\LaravelUrlShortener\\TLYServiceProvider" --tag=config 24 | ``` 25 | 26 | Set your API token in `.env`: 27 | 28 | ```plaintext 29 | TLY_API_TOKEN=your_api_token_here 30 | ``` 31 | 32 | ## Usage 33 | 34 | ### Create a Short Link 35 | 36 | ```php 37 | use TLY\LaravelUrlShortener\Facades\TLYApi; 38 | 39 | $response = TLYApi::create([ 40 | 'long_url' => 'https://example.com', 41 | 'description' => 'Example Link', 42 | ]); 43 | 44 | 45 | ``` 46 | 47 | ### Update a Short Link 48 | 49 | ```php 50 | use TLY\LaravelUrlShortener\Facades\TLYApi; 51 | 52 | $response = TLYApi::update([ 53 | 'short_url' => 'https://t.ly/123', 54 | 'long_url' => 'https://new-destination.com', 55 | 'description' => 'Updated Link Description', 56 | 'expire_at_datetime' => '2035-12-31 23:59:59', 57 | ]); 58 | 59 | ``` 60 | 61 | ### Delete a Short Link 62 | 63 | ```php 64 | use TLY\LaravelUrlShortener\Facades\TLYApi; 65 | 66 | $response = TLYApi::delete('https://t.ly/123'); 67 | 68 | ``` 69 | 70 | ### Get a Short Link 71 | 72 | ```php 73 | use TLY\LaravelUrlShortener\Facades\TLYApi; 74 | 75 | $response = TLYApi::get('https://t.ly/123'); 76 | 77 | ``` 78 | 79 | ### Get the stats for a Short Link 80 | 81 | ```php 82 | use TLY\LaravelUrlShortener\Facades\TLYApi; 83 | 84 | $response = TLYApi::stats('https://t.ly/123'); 85 | 86 | ``` 87 | 88 | ## License 89 | 90 | This package is licensed under the MIT License. 91 | --------------------------------------------------------------------------------