spatie/laravel-url-ai-transformer
Laravel package to transform URLs and their web content with AI. Extract structured data (JSON-LD), generate summaries, images, or custom outputs via transformers and prompts. Runs via an Artisan command and stores results in the database for later retrieval.
Let's build a simple example that transforms blog posts into structured data. We'll use the LdJsonTransformer that comes with the package to extract structured information from web pages.
First, you should use the Transform class to register URLs to transform, and which transformer to use:
use Spatie\LaravelUrlAiTransformer\Support\Transform;
use Spatie\LaravelUrlAiTransformer\Transformers\LdJsonTransformer;
// typically, in a service provider
Transform::urls(
'https://spatie.be/blog',
'https://spatie.be/open-source',
'https://spatie.be/about-us'
)->usingTransformers(new LdJsonTransformer)
Now, you can transform the URLs by running:
php artisan transform-urls
This command will dispatch a queued job for each URL. Inside that queued job:
transformation_results table.The transformation_results table stores all transformation data with the following fields:
The latest_exception fields will be cleared when the transformation completes successfully.
Here's how you can retrieve transformation results in your application.
use Spatie\LaravelUrlAiTransformer\Models\TransformationResult;
// Get structured data for a specific URL
$ldJsonData = TransformationResult::forUrl('https://spatie.be/blog', 'ldJson');
The first parameter is the URL, the second parameter is the transformer type. By default, transformer type is the lowercased class name of the transformer without the Transformer suffix.
To keep your transformations up to date, schedule the command to run periodically:
// In app/Console/Kernel.php
protected function schedule(Schedule $schedule)
{
$schedule->command('transform-urls')
->daily()
->at('02:00');
}
How can I help you explore Laravel packages today?