PHP code example of richardowen / url-shortener

1. Go to this page and download the library: Download richardowen/url-shortener library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

richardowen / url-shortener example snippets



// test.php
\UrlShortener;
use UrlShortener\Storage\MemoryStorage;
use UrlShortener\Generator\RandomCodeGenerator;

echo "🚀 Testing URL Shortener Library...\n";

try {
    $storage = new MemoryStorage();
    $generator = new RandomCodeGenerator();
    $shortener = new UrlShortener($storage, $generator);
    
    $testUrl = 'https://packagist.org/packages/richardowen/url-shortener';
    $shortCode = $shortener->shorten($testUrl);
    $expandedUrl = $shortener->expand($shortCode);
    
    if ($expandedUrl === $testUrl) {
        echo "✅ Installation successful!\n";
        echo "Short code: {$shortCode}\n";
        echo "Original URL: {$expandedUrl}\n";
    } else {
        echo "❌ Something went wrong!\n";
    }
} catch (Exception $e) {
    echo "❌ Error: " . $e->getMessage() . "\n";
}


UrlShortener\UrlShortener;
use UrlShortener\Storage\FileStorage;
use UrlShortener\Generator\RandomCodeGenerator;

// Create storage and code generator
$storage = new FileStorage(__DIR__ . '/data');
$generator = new RandomCodeGenerator();

// Initialize the URL shortener
$shortener = new UrlShortener($storage, $generator);

// Shorten a URL
$shortCode = $shortener->shorten('https://www.example.com/very/long/url');
echo "Short code: " . $shortCode . "\n";

// Expand the short code
$originalUrl = $shortener->expand($shortCode);
echo "Original URL: " . $originalUrl . "\n";

// Get analytics
$analytics = $shortener->getAnalytics($shortCode);
print_r($analytics);

use UrlShortener\Storage\FileStorage;

$storage = new FileStorage('/path/to/data/directory');

use UrlShortener\Storage\MemoryStorage;

$storage = new MemoryStorage();

use UrlShortener\Storage\DatabaseStorage;

$pdo = new PDO('mysql:host=localhost;dbname=url_shortener', $username, $password);
$storage = new DatabaseStorage($pdo);

use UrlShortener\Generator\RandomCodeGenerator;

$generator = new RandomCodeGenerator(6); // 6 character codes

use UrlShortener\Generator\GeneratorInterface;

class CustomGenerator implements GeneratorInterface
{
    public function generate(): string
    {
        // Your custom logic here
        return 'custom-' . uniqid();
    }
}

// Get basic analytics
$analytics = $shortener->getAnalytics($shortCode);

// Analytics // - Geographic data (if implemented)

$metadata = ['campaign' => 'summer2025', 'source' => 'email'];
$shortCode = $shortener->shorten($campaignUrl, null, $metadata);

$expiresAt = new DateTimeImmutable('+7 days');
$shortCode = $shortener->shorten($temporaryUrl, $expiresAt);

$socialUrl = 'https://blog.example.com/how-to-build-url-shortener';
$shortCode = $shortener->shorten($socialUrl);
echo "Share: https://yourdomain.com/" . $shortCode;
bash
git clone https://github.com/RichardOwen2/url-shortener-php.git
cd url-shortener-php
composer install
bash
php test.php