hpatoio/bitly-api
Unmaintained PHP Bitly API client built on Guzzle. Provides a Bitly\Client for calling endpoints (e.g., Highvalue), supports custom cURL options like timeouts, and allows attaching Guzzle plugins (e.g., logging). Symfony2 integration mentioned.
composer require hpatoio/bitly-api
use Hpatoio\BitlyApi\BitlyApi;
$bitly = new BitlyApi('YOUR_ACCESS_TOKEN');
$shortUrl = $bitly->shorten('https://example.com');
echo $shortUrl->getShortUrl();
spatie/bitly or direct API integration for production use.src/BitlyApi.php – Core class with API methods (may contain outdated endpoints).src/Exceptions/ – Custom exceptions for error handling.src/Models/ – Response models (e.g., ShortUrl, Click).README.md or CHANGELOG.md for deprecation warnings and migration paths.URL Shortening (Legacy API):
$bitly = new BitlyApi($accessToken);
$result = $bitly->shorten('https://laravel.com/docs', [
'title' => 'Laravel Docs',
'tags' => ['laravel', 'docs']
]);
Fetching Analytics (Legacy):
$clicks = $bitly->getClicks('bit.ly/3Example');
foreach ($clicks as $click) {
echo $click->getTimestamp() . ': ' . $click->getCountry();
}
try-catch for BitlyException to handle deprecated endpoint errors gracefully.Batch Operations (Legacy):
$urls = ['https://example1.com', 'https://example2.com'];
$shortUrls = $bitly->shortenBatch($urls);
Laravel Service Provider (Legacy): Bind the client to the container:
$this->app->singleton(BitlyApi::class, function ($app) {
return new BitlyApi(config('services.bitly.token'));
});
spatie/bitly) in production.Configuration:
Store the access token in .env:
BITLY_ACCESS_TOKEN=your_token_here
env() or a secrets manager.Error Handling: Wrap API calls to handle deprecation warnings:
try {
$result = $bitly->shorten('https://example.com');
} catch (\Hpatoio\BitlyApi\Exceptions\BitlyException $e) {
Log::warning('Deprecated Bitly API call: ' . $e->getMessage());
// Fallback to direct API call or alternative package
}
Deprecated API (Critical):
2.0.6 release explicitly warns about deprecation.spatie/bitly or direct API integration.Rate Limiting:
$cacheKey = 'bitly:shortened:' . md5($url);
if (Cache::has($cacheKey)) {
return Cache::get($cacheKey);
}
$shortUrl = $bitly->shorten($url);
Cache::put($cacheKey, $shortUrl, now()->addHours(1));
Token Management:
.env or a secrets manager.Response Parsing:
ShortUrl and Click models may not align with Bitly’s v4 response structure.$response = $bitly->client->get('/v4/shorten', ['query' => ['long_url' => $url]]);
$data = json_decode($response->getBody(), true);
Enable Guzzle Debugging: Log requests/responses to identify deprecated endpoints:
$bitly = new BitlyApi($token, [
'debug' => true,
'handler' => HandlerStack::create([
new \GuzzleHttp\Middleware::tap(function ($request) {
Log::debug('Deprecated Bitly Request:', [
'url' => (string) $request->getUri(),
'method' => $request->getMethod()
]);
}),
]),
]);
Common Errors:
401 Unauthorized: Invalid token or deprecated endpoint.404 Not Found: Endpoint no longer exists in v4.429 Too Many Requests: Hit rate limits.410 Gone to catch explicitly deprecated endpoints.Migrate to Bitly API v4: Override the base URL and endpoints:
class BitlyV4Api extends BitlyApi {
protected $baseUri = 'https://api-ssl.bitly.com/v4';
public function shorten($longUrl, array $options = []) {
return $this->client->post('/shorten', [
'json' => array_merge(['long_url' => $longUrl], $options)
]);
}
}
Webhooks (Legacy): Bitly’s webhook system has changed in v4. Update validation logic if using this feature.
Testing: Mock the client to test deprecated behavior:
$mockHandler = HandlerStack::create();
$mockHandler->push(Middleware::mock(function ($request) {
if (str_contains($request->getUri(), 'v3')) {
return new Response(410, [], json_encode(['error' => 'Deprecated']));
}
return new Response(200, [], json_encode(['id' => 'bit.ly/test']));
}));
$bitly = new BitlyApi($token, ['handler' => $mockHandler]);
Fallback Logic: Implement a fallback to direct API calls when the package fails:
public function safeShorten($url) {
try {
return $this->shorten($url);
} catch (\Exception $e) {
return $this->directApiCall($url); // Custom v4 implementation
}
}
How can I help you explore Laravel packages today?