Skip to content
Advertisement

Transforming scraper class code into Laravel command

I want to convert a website scraper that I made in basic PHP, which is working fine, to Laravel.

My question is, how to accomplish this? A friend of mine told me something about php artisan make:command, but I never used this before and neither did he. Can you give me some pointers on how to use this? Any helpful information? Or is there any other way to create this ?

What I have, basically, will be a cron job that will run once a day to update my database with relevant info.

Advertisement

Answer

To perform a repeated task in Laravel, the simplest way is to use Task Scheduling. You use your crontab to call an artisan command every minute, which handles all the scheduling for you; this is explained well in the documentation.

Then you place the code you want to run every day inside the schedule function, much like below.

protected function schedule(Schedule $schedule)
{

    $schedule->call(function () {
        // Your scraping function here
    })->daily();
}
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement