有没有理由说这个cron工作不起作用

时间:2012-12-01 14:55:18

标签: php javascript cron crontab

编辑:Cron行:/ usr / bin / php /usr/local/yy/yy/yy/webspace/httpdocs/test.mysite.ie/test.php> / dev / null 2>& 1

我编写了一个脚本,当我在浏览器中导航时,它的功能应该是正常的。这是我第一次尝试使用cron工作,所以我不太熟悉它们的工作方式。脚本如下。正如我所说,如果我在Web浏览器中导航到URL,脚本将按原样运行。

test.php的

<script src="jquery.min.js"></script>
<script>
//SET UP JS VARIABLES
var allMatchedNumbers = new Array();
var matchedthingyNumbers;
var matchedthingyPlus1Numbers;
var matchedthingyPlus2Numbers;
var winningthingyNumbers = new Array();
var winningBonusNumber;
var winningthingyPlus1Numbers = new Array();
var winningPlus1BonusNumber;
var winningthingyPlus2Numbers = new Array();
var winningPlus2BonusNumber;
var thingyList;
var thingyListItems;
var thingyPlus1List;
var thingyPlus1ListItems;
var thingyPlus2List;
var thingyPlus2ListItems;
var userNumbers = new Array();
var displayCounter = 1;
var drawDate;
var thingyNumbers;
var thingyBonus;
var thingyPlus1;
var thingyPlus1Bonus;
var thingyPlus2;
var thingyPlus2Bonus;

//GET RESULTS & DATE FOR thingy, PLUS1, PLUS2 FROM THE DOM OBJECT ONLY
$(document).ready(function fetchResults(){
    $.ajax({
        url: "scrape_page.php",
        success: function(data) {
        } 
    });

    $.ajax({
        url: "latest_results.txt",
        success: function(data) {
            var dom = $(data);
            //GET thingy DATE
            drawDate = dom.find('.date-heading.fooRegular').contents().first().text();
            //GET thingy NUMBERS
            thingyNumbers = dom.find('.result-block').eq(0).find('.thingy-winning-numbers');
            thingyBonus = dom.find('.result-block').eq(0).find('.thingy-bonus');
            thingyPlus1 = dom.find('.result-block').eq(1).find('.thingy-winning-numbers');
            thingyPlus1Bonus = dom.find('.result-block').eq(1).find('.thingy-bonus');
            thingyPlus2 = dom.find('.result-block').eq(2).find('.thingy-winning-numbers');
            thingyPlus2Bonus = dom.find('.result-block').eq(2).find('.thingy-bonus');
            populateWinningNumbers();
        } 
    });
});


//PUT WINNING NUMBERS IN ARRAY
function populateWinningNumbers() {
    //MAIN thingy NUMBERS
    thingyList = thingyNumbers;
    thingyListItems = thingyList.find('li');
    thingyPlus1List = thingyPlus1;
    thingyPlus1ListItems = thingyPlus1List.find('li');
    thingyPlus2List = thingyPlus2;
    thingyPlus2ListItems = thingyPlus2List.find('li');

    thingyListItems.each(function(index) {
        winningthingyNumbers[index] = parseInt($(this).text());
    });
    //winningBonusNumber = parseInt($('#mainthingyBonus').find('li').text());
    winningBonusNumber = parseInt($(thingyBonus).find('li').text());
    winningthingyNumbers.push(winningBonusNumber);

    //thingy PLUS NUMBERS
    thingyPlus1ListItems.each(function(index) {
        winningthingyPlus1Numbers[index] = parseInt($(this).text());
    });
    winningPlus1BonusNumber = parseInt($(thingyPlus1Bonus).find('li').text());
    winningthingyPlus1Numbers.push(winningPlus1BonusNumber);
    //PLUS 2
    thingyPlus2ListItems.each(function(index) {
        winningthingyPlus2Numbers[index] = parseInt($(this).text());
    });
    winningPlus2BonusNumber = parseInt($(thingyPlus1Bonus).find('li').text());
    winningthingyPlus2Numbers.push(winningPlus2BonusNumber);

    postDataToDB();
}

//POST OFFICIAL thingy NUMBERS TO DATABASE
function postDataToDB() {
    $.ajax({
        url: "postToDB.php",
        type: "post",
        data: {thingyNums:winningthingyNumbers, thingyPlus1Nums: winningthingyPlus1Numbers, thingyPlus2Nums: winningthingyPlus2Numbers, drawDate:drawDate},
        // callback handler that will be called on success
        success: function (data) {
        }
    }); 
}
</script>

scrape_page.php

<?php

    include 'simple_html_dom.php';

    $html = file_get_html('http://www.site.com');
    $file = 'latest_results.txt';
    file_put_contents($file, $html);
?>

postToDB.php

<?php

$winningNums = $_POST['thingyNums'];
$winningPlus1Nums = $_POST['thingyPlus1Nums'];
$winningPlus2Nums = $_POST['thingyPlus2Nums'];
$drawDate = $_POST['drawDate'];

$thingyToSave = implode(',', $winningNums);
$plus1ToSave = implode(',', $winningPlus1Nums);
$plus2ToSave = implode(',', $winningPlus2Nums);

//CONNECT TO REMOTE
$con = mysql_connect("172.xx.xx.xx","user","pass");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

//SELECT thingy DB
mysql_select_db("App", $con);

//CHECK IF DATE ALREADY EXISTS IN DB
$date_check = mysql_query("SELECT drawDate FROM thingy WHERE drawDate='$drawDate'");
$do_date_check = mysql_num_rows($date_check);

if($do_date_check > 0){
    //DATE ALREADY IN DB
    die("Entries already exist");
} else {
    mysql_query("INSERT INTO thingy (drawDate) VALUES ('$drawDate')");
    mysql_query("UPDATE thingy SET thingyRes = '$thingyToSave' WHERE drawDate = '$drawDate'");
    mysql_query("UPDATE thingy SET thingyPlus1Res = '$plus1ToSave' WHERE drawDate = '$drawDate'");
    mysql_query("UPDATE thingy SET thingyPlus2Res = '$plus2ToSave' WHERE drawDate = '$drawDate'");
    echo "Success";
}

mysql_close($con);
?>

2 个答案:

答案 0 :(得分:2)

如果您通过test.php或类似方式致电wget,该工具 php中没有JavaScript引擎。因此,页面的任何依赖于JavaScript的功能都将无法运行。

工具可以加载页面并在其中执行JavaScript。他们被称为“无头”浏览器。例如,PhantomJS,这是一个基于WebKit的无头浏览器,里面有一个JavaScript引擎。还有无头版的Firefox等。

您的Web服务器正常运行,并将无头浏览器指向页面的URL,这将触发PHP(就像浏览器请求页面一样)并处理客户端页面中的JavaScript。

答案 1 :(得分:2)

您尝试运行的脚本包含Javascript - 在浏览器中执行。 Cron将在服务器上执行 PHP 脚本,并且无处发送输出(因为您将其指向/dev/null)。

在该场景中没有任何内容可以解释和执行Javascript。

您需要将Javascript中的逻辑(它向两个相关的PHP脚本发出请求)移植到PHP。 (你可以运行一些服务器端的javascript解释器/ php扩展,但在这种情况下,这看起来有点疯狂。