我有一个php脚本,它执行一个函数并创建一个包含结果的xml文件。我希望这个脚本每天只运行一次,我正在使用wordpress cron来实现这一点。问题是这个脚本总是在所有页面加载时运行,这很糟糕,因为它需要超过30秒才能完成。如何停止它始终运行并使其仅与wp cron一起工作?我读过关于cronjob的文章,但我的主机没有这个功能。这是我在php文件顶部使用的代码:
add_action( \'my_cron_hook\', \'myfunction\' );
if( !wp_next_scheduled( \'my_cron_hook\' ) ) {
wp_schedule_event( time(), \'daily\', \'my_cron_hook\' );
}
register_deactivation_hook( __FILE__, \'my_deactivate\' );
function my_deactivate() {
$timestamp = wp_next_scheduled( \'my_cron_hook\' );
wp_unschedule_event($timestamp, \'my_cron_hook\' );
}
我希望脚本每天运行并始终在后台运行,它不应该影响用户导航。谢谢
编辑
以下是所有代码脚本:
add_action( \'cO_cron_hook\', \'remoteHeader\' );
if( !wp_next_scheduled( \'cO_cron_hook\' ) ) {
wp_schedule_event( time() + 24 * 60 * 60, \'daily\', \'cO_cron_hook\' );
}
register_deactivation_hook( __FILE__, \'bl_deactivate\' );
function bl_deactivate() {
$timestamp = wp_next_scheduled( \'cO_cron_hook\' );
wp_unschedule_event($timestamp, \'cO_cron_hook\' );
}
set_time_limit(300);
function remoteHeader($url){
$online = \'online\';
$offline = \'offline\';
$agent = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Win64; x64; Trident/5.0)";
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL,$url );
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch,CURLOPT_VERBOSE,false);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_NOBODY, 1);
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch,CURLOPT_SSLVERSION,3);
curl_setopt($ch,CURLOPT_SSL_VERIFYHOST, FALSE);
curl_exec($ch);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if($httpcode >= 200 && $httpcode < 400) return $online;
else return $httpcode;
}
/* create a dom document with encoding utf8 */
$domtree = new DOMDocument(\'1.0\', \'UTF-8\');
/* create the root element of the xml tree */
$xmlRoot = $domtree->createElement("xml");
/* append it to the document created */
$xmlRoot = $domtree->appendChild($xmlRoot);
$currentSite = $domtree->createElement("site");
$currentSite = $xmlRoot->appendChild($currentSite);
/* you should enclose the following two lines in a cicle */
$site = remoteHeader("http://google.com/");
$currentTrack->appendChild($domtree->createElement(\'google\',$site));
$site2 = remoteHeader("http://wordpress.com/");
$currentTrack->appendChild($domtree->createElement(\'wordpress\',$site2));
...
header("Content-Type: text/plain");
/* get the xml printed */
echo $domtree->save(\'mytest.xml\')