【发布时间】:2014-12-05 05:20:10
【问题描述】:
当您在浏览器中手动访问页面时,我有一个 HTML 文件会触发两个 PHP 脚本。当我手动输入 URL 时,一切正常。我想知道如何将这个脚本作为 cron 运行,以便它在给定的时间间隔内不断触发?
到目前为止,我已经尝试过 wget 和 curl,但没有成功。任何帮助将不胜感激。
<!DOCTYPE html>
<html>
<head>
<title></title>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script src="js/jquery.shoutcast.min.js"></script>
<link rel="stylesheet" type="text/css" href="http://fonts.googleapis.com/css?family=Kaushan Script">
<!-- Current Song Played -->
<script>
// Get current song playing and load it into an element with an ID of songtitle
$.SHOUTcast({
host : 'live.soundcheck.xyz',
port : 8000,
interval : 5000,
}).stats(function(){
$('#songtitle').text(this.get('songtitle'));
$(document).ready(function() {
// console.log("Document Ready!");
var content = $('#songtitle').text();
var music = $('#songtitle').text();
var song = $('#songtitle').text();
var title = $('#songtitle').text();
// console.log(content);
// $.post("nowplaying.php", //Required URL of the page on server
$.post("ignore.php", //Required URL of the page on server
{ // Data Sending With Request To Server
name:content,
},
function(response){ // Required Callback Function
// alert("Response: " + response); // "response" receives - whatever written in echo of above PHP script.
});
$.post("new/update.php", //Required URL of the page on server
{ // Data Sending With Request To Server
track:music,
},
function(response){ // Required Callback Function
// alert("Response: " + response); // "response" receives - whatever written in echo of above PHP script.
// console.log(response);
});
/*$.post("current.php", //Required URL of the page on server
{ // Data Sending With Request To Server
current:song,
},
function(response){ // Required Callback Function
// alert("Response: " + response); // "response" receives - whatever written in echo of above PHP script.
console.log(response);
});*/
});
});
</script>
<style type="text/css">
#nowplaying{
border: solid;
border-color: black;
width: 750px;
height: auto;
padding: 5px 5px 5px 5px;
float: left;
}
#label{
font-weight: 500;
font-family: Kaushan Script;
font-size: xx-large;
}
#songtitle{
font-weight: 300;
font-family: fantasy;
font-size: medium;
}
ol.labels-list {
list-style-type: none;
margin: 0;
margin-left: 1em;
padding: 0;
counter-reset: li-counter;
}
ol.labels-list > li{
position: relative;
margin-bottom: 1.5em;
padding: 1.5em;
background: #45484d; /* Old browsers */
background: -moz-linear-gradient(left, #45484d 0%, #000000 100%); /* FF3.6+ */
background: -webkit-gradient(linear, left top, right top, color-stop(0%,#45484d), color-stop(100%,#000000)); /* Chrome,Safari4+ */
background: -webkit-linear-gradient(left, #45484d 0%,#000000 100%); /* Chrome10+,Safari5.1+ */
background: -o-linear-gradient(left, #45484d 0%,#000000 100%); /* Opera 11.10+ */
background: -ms-linear-gradient(left, #45484d 0%,#000000 100%); /* IE10+ */
background: linear-gradient(to right, #45484d 0%,#000000 100%); /* W3C */
}
ol.labels-list > li:before {
position: absolute;
top: -0.3em;
left: -0.5em;
width: 1.8em;
height: 1.2em;
font-size: 2em;
line-height: 1.2;
font-weight: bold;
text-align: center;
color: #464646;
background-color: #d0d0d0;
transform: rotate(-20deg);
-ms-transform: rotate(-20deg);
-webkit-transform: rotate(-20deg);
z-index: 99;
overflow: hidden;
content: counter(li-counter);
counter-increment: li-counter;
}
.lastplayed{
font-weight: 300;
font-family: Kaushan Script;
font-size: medium;
color: white;
}
#imgholder{
width: 50%;
margin: 0 auto;
}
.text-glow{
color: #000000;
font-family: Arial;
font-size: 4em;
margin: 0 auto;
text-align: center;
width: 1000px;
padding-top:75px;
}
#nowplaying{
text-align: center;
}
.text-glow:hover,
.text-glow:focus
.text-glow:active{
-webkit-stroke-width: 5.3px;
-webkit-stroke-color: #000000;
-webkit-fill-color: #FFFFFF;
text-shadow: 1px 0px 20px #F90;
-webkit-transition: width 0.3s; /*Safari & Chrome*/
transition: width 0.3s;
-moz-transition: width 0.3s; /* Firefox 4 */
-o-transition: width 0.3s; /* Opera */
}
.text-glow a{
-webkit-transition: all 0.3s ease-in; /*Safari & Chrome*/
transition: all 0.3s ease-in;
-moz-transition: all 0.3s ease-in; /* Firefox 4 */
-o-transition: all 0.3s ease-in; /* Opera */
text-decoration:none;
color:#000000;
}
#songtitle{
font-size: x-large;
}
</style>
</head>
<body>
<div id="reload"><span id="songtitle" class="now"></span></div>
<div id="php"><?php echo date('l, F jS, Y'); ?></div>
<div id="php2"><?php echo $_POST["name"]; ?></div>
</body>
【问题讨论】:
-
它的js,你需要一个浏览器才能运行它。仍然可以通过 cron 完成,但它很痛苦 - 你可以将它转换为纯服务器端吗?
-
您可能想查看Codeception 之类的内容并为其编写测试。 Codeception 使用 phpBrowser 并且可以处理诸如 javascript 之类的东西。然后就像@Dagon 说的那样,只需安排一个 cron 来启动测试,无论你喜欢什么频率。
-
这真的很不现实,但是你可以用支持现代js的基于文本的浏览器打开页面(通过cron)。 elinks 是我所知道的唯一一个,如果用 spidermonkey 编译的话