0

我在这里使用此代码从我的 Minecraft 服务器获取信息。它使用静态 .php 文件按预期完成,但在将其移动到 WordPress 的本地副本后,我收到以下错误:

Fatal error: Call to a member function GetPlayers() on a non-object in C:\...\home.php on line 125

我很确定我正确地移动了所有东西。在第 125 行,是这段代码:

<div class="online group container">
      <h2> Who's online </h2>
          <?php if( ( $Players = $Query->GetPlayers( ) ) !== false ): ?>
          <?php foreach( $Players as $Player ): ?>
          <div class="player">                      
            <span class="p"  title="<?php echo htmlspecialchars( $Player ); ?>" data-player="<?php echo htmlspecialchars( $Player ); ?>">☺&lt;/span>
          </div><!-- /.player -->

这就是我在 doctype 之前在 header.php 中调用 PHP 类的方式。

<?php

  define( 'MQ_SERVER_ADDR', '108.170.3.138:25643' );
  define( 'MQ_SERVER_PORT', 25643 );
  define( 'MQ_TIMEOUT', 1 );

  require bloginfo('template_url') . 'inc/avatars/MinecraftQuery.class.php';

  $Timer = MicroTime( true );
  $Query = new MinecraftQuery( );

  try
  {
    $Query->Connect( MQ_SERVER_ADDR, MQ_SERVER_PORT, MQ_TIMEOUT );
  }
  catch( MinecraftQueryException $e )
  {
    $Error = $e->getMessage();
   echo 'error. <br>'. $Error;
  }

?>

这很奇怪,因为它在没有 WordPress 的情况下工作得很好,但是一旦我移动它,它就坏了。我究竟做错了什么?

4

1 回答 1

1

创建将返回查询的函数

函数.php

function get_MQ_Query() {
  define( 'MQ_SERVER_ADDR', '108.170.3.138:25643' );
  define( 'MQ_SERVER_PORT', 25643 );
  define( 'MQ_TIMEOUT', 1 );

  require bloginfo('template_url') . 'inc/avatars/MinecraftQuery.class.php';

  $Timer = MicroTime( true );
  $Query = new MinecraftQuery( );

  try
  {
    $Query->Connect( MQ_SERVER_ADDR, MQ_SERVER_PORT, MQ_TIMEOUT );
  }
  catch( MinecraftQueryException $e )
  {
    $Error = $e->getMessage();
   echo 'error. <br>'. $Error;
  }
  return $Query;
}

主页.php

$Query = get_MQ_Query();
?><div class="online group container">
  <h2> Who's online </h2>
于 2012-05-25T12:47:14.360 回答