普通网友 2019-05-06 23:26
浏览 63

获取从具有cron作业的网站获取的数据的更新结果的最佳方法

I created a simple mobile app using React Native and for the data source, I created a simple API in PHP which fetches the data from a website and provides it to the users. Now I wanted to add notifications feature in my app and for that, I always need to know which data has been updated, I solved this problem by storing my data in two different tables in my DB and then comparing the changes, which is a lengthy procedure and not very efficient in my opinion. All my previous code was written in pure PHP without using any framework. Now I wanted to implement my whole project into Laravel based backend and don't know how can I achieve the functionality means getting updated data and then using that data to further send notifications to the users.

Summary of the functionality I want: - Parse data from a website - Look for new data - If there is new data send notifications -setting cron job to repeat

My old compare script looks like this:

<?PHP
include '../config/dbData.php';

// required headers
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");

$connect = mysqli_connect($HostName, $HostUser, $HostPass);
mysqli_select_db($connect, $DatabaseName);

mysqli_query($connect, "SET NAMES 'UTF8'") or die("ERROR: " . mysqli_error($connect));

$fetch3 = mysqli_query($connect, "SELECT * FROM timetable
                                    WHERE id NOT IN (SELECT id FROM TimetableStudentCompare)

                                    UNION

                                    SELECT * FROM TimetableStudentCompare
                                    WHERE id NOT IN (SELECT id FROM timetable)

                                    UNION

                                    SELECT * FROM timetable
                                    WHERE Vertreter NOT IN (SELECT Vertreter FROM TimetableStudentCompare)

                                    UNION

                                    SELECT * FROM TimetableStudentCompare
                                    WHERE Vertreter NOT IN (SELECT Vertreter FROM timetable)

                                    UNION

                                    SELECT * FROM timetable
                                    WHERE Std NOT IN (SELECT Std FROM TimetableStudentCompare)

                                    UNION

                                    SELECT * FROM TimetableStudentCompare
                                    WHERE Std NOT IN (SELECT Std FROM timetable)

                                    UNION

                                    SELECT * FROM timetable
                                    WHERE Klasse NOT IN (SELECT Klasse FROM TimetableStudentCompare)

                                    UNION

                                    SELECT * FROM TimetableStudentCompare
                                    WHERE Klasse NOT IN (SELECT Klasse FROM timetable)");

$array3 = array();
while ($row3 = mysqli_fetch_assoc($fetch3)) {
    $array3[] = $row3;
}
echo json_encode($array3, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);

// Fetching Klassen to a new array
$KlasseArray = array();

foreach ($array3 as $value) {
    array_push($KlasseArray, $value['Klasse']);
}
// array only with classes
print_r($KlasseArray);

//Removing Duplicates
$list = $KlasseArray;
sort($list);
foreach ($list as $k => $v) {
    if (isset($check)) {
        if ($check === $v) {
            unset($list[$k]);
        }
    }
    $check = $v;
}

$noDuplicate = array_values($list);
// Result klassen with no duplicates
print_r($noDuplicate);

mysqli_close($connect);

Can anyone please help me with this?

</div>
  • 写回答

1条回答 默认 最新

  • dpus81500574 2019-05-07 03:13
    关注
    1. For storing you should use Model observer or if you want a package may be Laravel Auditing. this way you can avoid cron.

    2. You should not make database connection like that even cron should be part of your laravel app not separated, there is Job Scheduler, you can use for it.

    3. To run a job scheduler to update database, you can define an artisan command, that should hold your logic to update your DB and it will also help you in testing as you can just run the artisan command and test the query.

    4. To generate an artisan command

      php artisan make:command YourTableUpdate

    And the class will be generated in console/ directory like

    <?php
    
    namespace App\Console\Commands;
    
    use Illuminate\Console\Command;
    
    class YourTableUpdate extends Command
    {
        /**
         * The name and signature of the console command.
         *
         * @var string
         */
        protected $signature = 'your-table:update';
    
        /**
         * The console command description.
         *
         * @var string
         */
        protected $description = 'Updates your table';
    
        /**
         * Create a new command instance.
         *
         * @return void
         */
        public function __construct()
        {
            parent::__construct();
        }
    
        /**
         * Execute the console command.
         *
         * @param  
         * @return mixed
         */
        public function handle()
        {
            //logic to update your table
        }
    }
    

    Now to create the scheduler

    In your App\Console\Kernel class

    <?php
    
    namespace App\Console;
    
    use Illuminate\Support\Facades\DB;
    use Illuminate\Console\Scheduling\Schedule;
    use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
    
    class Kernel extends ConsoleKernel
    {
        /**
         * The Artisan commands provided by your application.
         *
         * @var array
         */
        protected $commands = [
            App\Console\Commands\YourTableUpdate
        ];
    
        /**
         * Define the application's command schedule.
         *
         * @param  \Illuminate\Console\Scheduling\Schedule  $schedule
         * @return void
         */
        protected function schedule(Schedule $schedule)
        {
            $schedule->command('your-table:update')->daily();
        }
    }
    

    you only need to add the following Cron entry to your server.

    * * * * * cd /path-to-your-project && php artisan schedule:run >> /dev/null 2>&1
    

    If you are using SSH and having ubuntu server, you can do something like

    1. sudo crontab -e
    2. Edit it to add that cron entry.
    3. save it and it is done.
    评论

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP