A
A
Alexey Karpan2016-09-27 06:05:38
PHP
Alexey Karpan, 2016-09-27 06:05:38

How to pass a variable to the parser.php script and run it via cron?

The database stores the link added by the user for parsing (parsing the bulletin board). At the moment, it works like this - the user enters his personal account, sees a list of his added links for parsing, clicks the check link and the script is triggered (the page is parsed, ads are checked against the database, new ones are added to the database, sent to email). You need to redo it so that it checks itself every 15 minutes. You need to use CRON. How to remake an existing script so that you can transfer a variable to the script and run it automatically? Here is the existing code:

<?php
  if($_GET) {
    $id_mess = (int)$_GET['id']; //получаем id ссылки из базы пользователя
  }
    
  $text = get_browse_mess($id_mess); //берем текст ссылки для парсинга

  include('simple_html_dom.php');
    
  $a = $text['text'];
  $html = file_get_html($a); // парсим страницу

  foreach($html->find('.bull-item') as $a) {
    $item['id'] = $a->find('td',0)->getAttribute('data-bulletin-id'); // получаем id объявления
    $item['desc'] = $a->find('.descriptionCell .bulletinLink',0)->plaintext; // получаем описание объявления
    $articles[] = $item;	
  }

  foreach($articles as $x) {
        
      $id = $x['id'];
      $desc =  $x['desc'];

      $id_new = proverka_parse($id); //тут проверяются только новые и записываются в базу 

      for ($i = 0; $i < count($id_new); $i++) { //бежим по циклу и записываем в базу новые объявления

                mysql_query("INSERT INTO ".PREF."parse SET id_object = '$id_new[0]', str = '$desc'");

// ... тут отправка на email новых объявлений

          }

  }


  $html->clear(); 
  unset($html); 

  
?>

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
Evgeny Kalibrov, 2016-09-27
@dvguinf

The script does not need to be redone if it is working for you.
Just run it by cron and pass the necessary parameters to GET. For example like this:

*/15 * * * * curl --request GET 'http://exemple.ru/parser.php?id=1000'

D
Dmitry Entelis, 2016-09-28
@DmitriyEntelis

Read about queues.
The queue can be stored anywhere, even in the database.
Cron gets the data from the queue and starts working.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question