heise/shariff
PHP backend for Shariff that fetches social share counts without client-side requests to social networks. Supports Buffer, Facebook, Pinterest, Reddit, Xing, VK and more, with domain whitelisting and configurable caching (filesystem or Laminas adapters).
Install via Composer (PHP 8.1+ required):
composer require heise/shariff:^11.0
Create a dedicated route (e.g., routes/web.php):
Route::get('/shariff', [ShariffController::class, 'index']);
Basic Controller Implementation (app/Http/Controllers/ShariffController.php):
use Heise\Shariff\Backend;
class ShariffController extends Controller
{
public function index()
{
$options = [
'domains' => ['yourdomain.com'],
'services' => ['Facebook', 'Twitter', 'LinkedIn'], // Updated default services (StumbleUpon removed)
'cache' => ['ttl' => 3600]
];
$shariff = new Backend($options);
$url = urldecode(request('url'));
$counts = $shariff->get($url);
return response()->json($counts);
}
}
Frontend Integration (unchanged):
<div class="shariff" data-url="https://yourdomain.com/article" data-services="facebook,twitter"></div>
<script src="https://cdn.jsdelivr.net/npm/shariff@latest/dist/shariff.min.js"></script>
$options = [
'Facebook' => [
'app_id' => env('FACEBOOK_APP_ID'),
'secret' => env('FACEBOOK_APP_SECRET'),
'graph_version' => 'v25.0' // Explicitly set (default in v11.0.0)
]
];
StumbleUpon and Flattr are no longer supported. Update frontend calls accordingly.'cacheClass' => \App\Services\LaravelCache::class,
'cache' => ['ttl' => 3600]
file, redis) is configured in .env.'domains' => [
'blog.yourdomain.com',
'news.yourdomain.com'
]
$services = explode(',', request('services', 'facebook,twitter,linkedin'));
$options['services'] = array_filter($services);
Backend to log service errors (now compatible with PHP 8.2+):
use Psr\Log\LoggerInterface;
class ShariffBackend extends Backend {
public function __construct(array $options, LoggerInterface $logger = null)
{
parent::__construct($options, $logger); // Logger is now optional in v11.0.0
}
}
# Example for Laravel Forge
forge php:8.2
php -v to confirm compatibility.'Facebook' => [
'app_id' => env('FACEBOOK_APP_ID'),
'secret' => env('FACEBOOK_APP_SECRET'),
'graph_version' => 'v25.0' // Explicitly set for clarity
]
StumbleUpon and Flattr are no longer supported. Frontend calls to these services will fail.stumbleupon from frontend data-services:
<div class="shariff" data-services="facebook,twitter">...</div>
'services' => ['Facebook', 'Twitter', 'LinkedIn'] // Example updated list
urldecode():
$url = urldecode(request('url'));
'cache' => [
'cacheDir' => storage_path('app/shariff-cache'),
'ttl' => 3600
]
Run:
mkdir -p storage/app/shariff-cache
chmod -R 775 storage/app/shariff-cache
Backend class to Laravel’s container (now type-hinted for PHP 8.2+):
// app/Providers/AppServiceProvider.php
public function register()
{
$this->app->singleton(Heise\Shariff\Backend::class, function ($app) {
return new Backend(config('shariff.options'), $app->make(LoggerInterface::class));
});
}
config/shariff.php):
return [
'options' => [
'domains' => ['yourdomain.com'],
'services' => ['Facebook', 'Twitter'],
'cache' => ['ttl' => 3600],
'Facebook' => [
'app_id' => env('FACEBOOK_APP_ID'),
'secret' => env('FACEBOOK_APP_SECRET'),
'graph_version' => 'v25.0'
]
]
];
'client' => [
'timeout' => 10.0,
'connect_timeout' => 3.0
]
http://invalid..com).public function test_shariff_with_invalid_url()
{
$shariff = new Backend(['domains' => ['example.com']]);
$this->expectException(\InvalidArgumentException::class);
$shariff->get('http://invalid..com');
}
'client' => [
'debug' => fopen(storage_path('logs/shariff-debug.log'), 'w')
]
Backend::get() to log outputs.// Middleware: HandleCors
public function handle($request, Closure $next)
{
return $next($request)
->header('Access-Control-Allow-Origin', '*')
->header('Access-Control-Allow-Methods', 'GET');
}
How can I help you explore Laravel packages today?