2013-07-11 106 views
0

我发现了一个检查url状态的脚本。现在我正在尝试将其与wordpress整合。这是代码:每天用wordpress执行php脚本

<?php 
/* 
Plugin Name: checkOnline Status 
Version: 0.1 
*/ 

add_action('cO_cron_hook', 'CheckRemoteService'); 

if(!wp_next_scheduled('cO_cron_hook')) { 
wp_schedule_event(time(), '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'); 
} 

function CheckRemoteService($atts) { 
extract(shortcode_atts(array(
    'url' => 'http://', 
    'cache' => '600', // 60*10 (10 minutes) 
    'online' => 'Online', // Custom online msg 
    'offline' => 'Offline' // Custom online msg 
), $atts)); 

$CachedStatus = 'cstatus_' . $url; 
$cachedposts = get_transient($CachedStatus); 
if ($cachedposts !== false) { 
return $cachedposts; 
} else { 

// Sometimes its best to change to a custom agent message 
// so you know where requests are coming from. 

$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, 5); 
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 $offline; 
    } 

set_transient($CachedStatus, $return, $cache); 
return $return; 
} 
} 
add_shortcode('checkmyurl','CheckRemoteService'); 

?> 

我想是每天只运行一次该代码。如何在刷新或访问使用简码的页面时使其无法运行?我需要大约50个站点的显示状态,并且页面需要超过10秒才能加载。 对不起,这个简单的问题(我是一个noob这个),但我找不到解决方案。 谢谢。

编辑。使用http://wordpress.org/plugins/crony/

+0

名称['cO _ ** cron ** _ hook'](https://en.wikipedia.org/wiki/Cron)是一个提示。 – PeeHaa

+0

WordPress的cron似乎有用,功能是预定的。我的问题是避免它在进入网站和刷新页面时加载。谢谢。 –

回答

1

解决您可以使用wp_schedule_event()安排重复执行的任务。

wp_schedule_event()documentation):

时刻表将由WordPress的动作芯上的特定间隔执行的钩,通过指定。如果预定的时间已过,某人访问您的WordPress网站时会触发此操作。查看插件API获取钩子列表。

每天安排一个事件,你会做这样的事情:

function someFunc() { 
    wp_schedule_event(time(), 'daily', 'mydailyEventHook'); 
} 

希望这有助于!

+0

如果我刷新页面,此块的脚本执行?谢谢。 –

+0

当我刷新页面时,我使用50个简码获取50个站点的状态,脚本始终运行(超过10秒)。所以我需要在访问该页面或刷新页面时阻止此脚本执行的操作。用户访问页面和脚本运行(超过10秒),用户刷新页面和脚本再次运行。用户只能看到来自cron的脚本结果。谢谢。 –

+0

我解决了这个问题:http://wordpress.org/plugins/crony/ –