所以我一直在尝试为wordpress编写这个小部件,以检查我本地托管的我的Minecraft服务器是否在线。我正在使用托管我的网站,只是我的代码有点问题,我不知道如何解决它。
<?php
define('PLUGIN_URL', plugin_dir_url( __FILE__ ));
add_action("widgets_init", array('sc_status', 'register'));
register_activation_hook( __FILE__, array('sc_status', 'activate'));
register_deactivation_hook( __FILE__, array('sc_status', 'deactivate'));
class sc_status {
function activate(){
$scdata = array( 'option1' => 'Default value' ,'option2' => 55);
if ( ! get_option('sc_status')){
add_option('sc_status' , $scdata);
} else {
update_option('sc_status' , $scdata);
}
}
function deactivate(){
delete_option('sc_status');
}
function control(){
$scdata = get_option('sc_status');
?>
<p><label>Title : <input name="widget_title" type="text" value="<?php echo $scdata['widget_title']; ?>" /></label></p>
<p><label>IP : <input name="server_ip" type="text" value="<?php echo $scdata['server_ip']; ?>" /></label></p>
<p><label>Port (25565) : <input name="server_port" size="5" type="text" value="<?php echo $scdata['server_port']; ?>" /></label></p>
<?php
if (isset($_POST['server_ip'])){
$scdata['widget_title'] = attribute_escape($_POST['widget_title']);
$scdata['server_ip'] = attribute_escape($_POST['server_ip']);
$scdata['server_port'] = attribute_escape($_POST['server_port']);
update_option('sc_status', $scdata);
}
}
function widget($args){
wp_enqueue_style('sc', PLUGIN_URL.'/css/sc_button.css');
$scdata = get_option('sc_status');
$title = $scdata['widget_title'];
$port = $scdata['server_port'];
if(empty($port)) {
$port = "25565";
}
echo $args['before_widget'];
if(!empty($title)) {
echo $args['before_title'] . $title . $args['after_title'];
}
// Check Server Status
$waitTimeoutInSeconds = 1;
if($fp = fsockopen($scdata['server_ip'],$port,$errCode,$errStr,$waitTimeoutInSeconds)){
// It worked
echo '<div class="btn btn-success" rel="tooltip" title="first tooltip">online</div>';
} else {
// It didn't work
echo '<div class="btn btn-danger">offline</div>';
}
fclose($fp);
// End
echo $args['after_widget'];
}
function register(){
register_sidebar_widget('Servercraft Status', array('sc_status', 'widget'));
register_widget_control('Servercraft Status', array('sc_status', 'control'));
}
}
这适用于“最”部分..问题是错误是:
警告:fsockopen()[function.fsockopen]:第62行无法连接(不想发布ip)(连接超时)
然后它发布了“离线”div,就像它应该......然后是
警告:fclose()要求参数1为资源,第69行给出布尔值
所以希望有人可以告诉我我做错了什么或如何解决它。
EDIT! 这是我从上面的部分做了几个修复后的代码。我仍然遇到的问题是服务器仍然正在反馈它离线的网站..它根本就没有。
<?php
define('PLUGIN_URL', plugin_dir_url( __FILE__ ));
add_action("widgets_init", array('sc_status', 'register'));
register_activation_hook( __FILE__, array('sc_status', 'activate'));
register_deactivation_hook( __FILE__, array('sc_status', 'deactivate'));
class sc_status {
function activate(){
$scdata = array( 'option1' => 'Default value' ,'option2' => 55);
if ( ! get_option('sc_status')){
add_option('sc_status' , $scdata);
} else {
update_option('sc_status' , $scdata);
}
}
function deactivate(){
delete_option('sc_status');
}
function control(){
$scdata = get_option('sc_status');
?>
<p><label>Title : <input name="widget_title" type="text" value="<?php echo $scdata['widget_title']; ?>" /></label></p>
<p><label>IP : <input name="server_ip" type="text" value="<?php echo $scdata['server_ip']; ?>" /></label></p>
<p><label>Port (25565) : <input name="server_port" size="5" type="text" value="<?php echo $scdata['server_port']; ?>" /></label></p>
<?php
if (isset($_POST['server_ip'])){
$scdata['widget_title'] = attribute_escape($_POST['widget_title']);
$scdata['server_ip'] = attribute_escape($_POST['server_ip']);
$scdata['server_port'] = attribute_escape($_POST['server_port']);
update_option('sc_status', $scdata);
}
}
function widget($args){
wp_enqueue_style('sc', PLUGIN_URL.'/css/sc_button.css');
$scdata = get_option('sc_status');
$title = $scdata['widget_title'];
$port = $scdata['server_port'];
if(empty($port)) {
$port = "25565";
}
echo $args['before_widget'];
if(!empty($title)) {
echo $args['before_title'] . $title . $args['after_title'];
}
// Check Server Status
$waitTimeoutInSeconds = 1;
$fp = @fsockopen($scdata['server_ip'],$port,$errCode,$errStr,$waitTimeoutInSeconds);
echo "<span class='sc_status'> IP : ".$scdata['server_ip']."</span>";
if($fp){
// It worked
echo '<div class="btn btn-success" rel="tooltip" title="first tooltip">online</div>';
fclose($fp);
} else {
// It didn't work
echo '<div class="btn btn-danger">offline</div>';
}
// End
echo $args['after_widget'];
}
function register(){
register_sidebar_widget('Servercraft Status', array('sc_status', 'widget'));
register_widget_control('Servercraft Status', array('sc_status', 'control'));
}
}
&GT;
答案 0 :(得分:0)
非常简单的解决方案:
if($fp = @fsockopen($scdata['server_ip'],$port,$errCode,$errStr,$waitTimeoutInSeconds)){
//it worked
fclose($fp);
}
您只需使用@隐藏错误消息。并且仅在打开时关闭连接(因此将fclose($fp);
移动到“已工作”部分。)