· 9 years ago · Sep 08, 2017, 08:20 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$cron = new cronjobs;
20$cron->insertUrl($_POST['url'],$_POST['intervalo']);
21*/
22
23class cronjobs{
24
25 private $dbuser = 'root';
26 private $dbpass = '';
27 private $db = 'cronjobs';
28
29
30public function __construct(){
31 $sql = mysql_connect('localhost',$this->dbuser, $this->dbpass);
32 mysql_select_db($this->bd,$sql)or die(mysql_error());
33 $this->sql = $sql;
34 }
35
36public function insertUrl($url,$time){
37 $searchUrl = mysql_query("SELECT url FROM urls WHERE url='".$url."'",$this->sql);
38 if(mysql_num_rows($searchUrl) == null){
39 $next = date('i',time()+($time*60));
40 mysql_query("INSERT INTO urls (url,time,next) VALUES('".$url."','".$time."','".$next."')",$this->sql);
41 }else{
42 mysql_query("UPDATE urls SET time='".$time."' WHERE url='".$url."'",$this->sql);
43 }
44 }
45
46public function exec(){
47 $searchUrl = mysql_query("SELECT * FROM urls WHERE next='".date('i')."'",$this->sql);
48 while($row = mysql_fetch_array($searchUrl)){
49 if(date('i') == $row['next']){
50 $next = date('i',time()+($row['time']*60));
51 mysql_query("UPDATE urls SET next='".$next."' WHERE url='".$row['url']."'",$this->sql);
52 $curl = curl_init();
53 curl_setopt($curl,CURLOPT_URL,$row['url']);
54 curl_setopt($curl,CURLOPT_FOLLOWLOCATION, true);
55 curl_exec($curl);
56 }
57 }
58 }
59}
60?>