Advertisement
Guest User

Untitled

a guest
May 17th, 2017
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.67 KB | None | 0 0
  1. <?php
  2. #Class cronjobs externo
  3. #Alan Cordeiro
  4. #3/04/2010
  5.  
  6. @Banco de Dados:
  7. /*
  8. CREATE TABLE IF NOT EXISTS `urls` (
  9.   `id` int(12) NOT NULL AUTO_INCREMENT,
  10.   `url` varchar(512) NOT NULL,
  11.   `time` int(128) NOT NULL,
  12.   `next` int(128) NOT NULL,
  13.   PRIMARY KEY (`id`)
  14. ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
  15. */
  16.  
  17. @Exemplo Inserir Tarefas:
  18. /*
  19. <?php
  20. $cron = new cronjobs;
  21. $cron->insertUrl($_POST['url'],$_POST['intervalo']);
  22. ?>
  23. */
  24.  
  25. class cronjobs{
  26.  
  27.     private $dbuser = 'root';
  28.     private $dbpass = '';
  29.     private $bd     = 'cronjobs';
  30.  
  31.  
  32. public function __construct(){
  33.         $sql = mysql_connect('localhost',$this->dbuser, $this->dbpass)or die(mysql_error());
  34.         mysql_select_db($this->bd,$sql)or die(mysql_error());
  35.         $this->sql = $sql; 
  36.     }
  37.  
  38. public function insertUrl($url,$time){
  39.         $searchUrl = mysql_query("SELECT url FROM urls WHERE url='".$url."'",$this->sql);
  40.         if(mysql_num_rows($searchUrl) == null){
  41.             $next = date('i',time()+($time*60));
  42.             mysql_query("INSERT INTO urls (url,time,next) VALUES('".$url."','".$time."','".$next."')",$this->sql);
  43.         }else{
  44.             mysql_query("UPDATE urls SET time='".$time."' WHERE url='".$url."'",$this->sql);
  45.         }
  46.     }
  47.  
  48. public function exec(){
  49.         $searchUrl = mysql_query("SELECT * FROM urls WHERE next='".date('i')."'",$this->sql);
  50.         while($row = mysql_fetch_array($searchUrl)){
  51.             if(date('i') == $row['next']){
  52.                 $next = date('i',time()+($row['time']*60));
  53.                 mysql_query("UPDATE urls SET next='".$next."' WHERE url='".$row['url']."'",$this->sql);
  54.                 $curl = curl_init();
  55.                 curl_setopt($curl,CURLOPT_URL,$row['url']);
  56.                 curl_setopt($curl,CURLOPT_FOLLOWLOCATION, true);
  57.                 curl_exec($curl);
  58.             }
  59.         }
  60.     }
  61. }
  62. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement