· 9 years ago · May 17, 2017, 11:14 PM
1<?php
2#Class cronjobs externo
3#Alan Cordeiro
4#3/04/2010
5
6@Banco de Dados:
7/*
8CREATE 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
25class cronjobs{
26
27 private $dbuser = 'root';
28 private $dbpass = '';
29 private $bd = 'cronjobs';
30
31
32public 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
38public 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
48public 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?>