PHP code example of palpalani / baylinks-laravel

1. Go to this page and download the library: Download palpalani/baylinks-laravel 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/ */

    

palpalani / baylinks-laravel example snippets


return [
    'server' => env('BAYLINKS_SERVER'),

    'api' => [
        'url' => 'api/v1',
        'key' => env('BAYLINKS_API_KEY'),
        'secret' => env('BAYLINKS_API_SECRET'),
    ],
];

use PalPalani\BayLinks\Facades\BayLinks;

$client = BayLinks::client();
$account = $client->accountDetails()->get('your_api_key');

use PalPalani\BayLinks\Facades\BayLinks;

$client = BayLinks::client();

$shortUrl = $client->createShortURL()->post('your_api_key', [
    'destination' => 'https://example.com/very-long-url',
    'domain' => 'custom.domain.com', // optional
]);

use PalPalani\BayLinks\Facades\BayLinks;

$client = BayLinks::client();

$bulkUrls = $client->createBulkURL()->post('your_api_key', [
    'destination' => [
        'https://example.com/page-1',
        'https://example.com/page-2',
        'https://example.com/page-3',
    ],
    'domain' => 'custom.domain.com', // optional
    'planet' => 'jupiter', // optional
    'expire' => 0, // optional (0 = never expires)
    'tag' => ['campaign' => 'summer-2024'], // optional metadata
]);

$client = BayLinks::client();

$response = $client->updateShortURLStatus()->post('your_api_key', [
    'url_id' => 'abc123',
    'status' => 'inactive',
]);

$client = BayLinks::client();

$visitRecords = $client->ShortUrlVisitRecord()->post('your_api_key', [
    'url_id' => 'abc123',
    'from_date' => '2024-01-01',
    'to_date' => '2024-12-31',
]);

use PalPalani\BayLinks\Factory;

class UrlShortenerService
{
    public function __construct(
        private Factory $bayLinks
    ) {}

    public function shortenUrl(string $url, string $apiKey): mixed
    {
        return $this->bayLinks
            ->createShortURL()
            ->post($apiKey, ['destination' => $url]);
    }
}

use Saloon\Exceptions\Request\FatalRequestException;
use Saloon\Exceptions\Request\RequestException;

try {
    $shortUrl = BayLinks::client()
        ->createShortURL()
        ->post('your_api_key', [
            'destination' => 'https://example.com',
        ]);
} catch (FatalRequestException $e) {
    // Handle fatal errors (network issues, timeouts, etc.)
    Log::error('BayLinks fatal error: ' . $e->getMessage());
} catch (RequestException $e) {
    // Handle API errors (validation, authentication, etc.)
    Log::error('BayLinks API error: ' . $e->getMessage());

    // Access response details
    $statusCode = $e->getStatus();
    $responseBody = $e->getResponse()->body();
}

use PalPalani\BayLinks\Facades\BayLinks;

$client = BayLinks::client();
// or
$client = BayLinks::factory();

[
    'destination' => 'https://example.com/page',  // ptional
]

[
    'destination' => [                              // xample.com/page-2',
    ],
    'domain' => 'custom.domain.com',               // optional
    'planet' => 'jupiter',                         // optional
    'expire' => 0,                                 // optional (seconds, 0 = never)
    'tag' => ['key' => 'value'],                   // optional (metadata)
]

// Test connection
try {
    $account = BayLinks::client()->accountDetails()->get('your_api_key');
    dump($account);
} catch (\Exception $e) {
    echo $e->getMessage();
}

// In config/baylinks-laravel.php (development only!)
// Note: Never disable SSL verification in production
bash
php artisan vendor:publish --tag="baylinks-laravel-config"
bash
vendor/bin/pest tests/ExampleTest.php
bash
composer analyse
bash
php artisan config:clear
php artisan cache:clear
composer dump-autoload