【问题标题】:PHP-Why do i get Undefined variable after using require_once? [closed]PHP-为什么我在使用 require_once 后得到未定义的变量? [关闭]
【发布时间】:2014-06-21 14:37:52
【问题描述】:

你好,我告诉他们我有以下问题: 我有 2 个文件,这是 php 中的一个类,另一个是调用此类并显示结果的脚本,有趣的是,在我的计算机上,localhost 在其他具有 php 5.4 的服务器上也可以正常运行,但在 hostmonster只工作了一天,第二天就停止工作了,我提供支持并说这是我的事情,队列错误代码给我的是:

PHP 注意:未定义变量:导致 /home/user/public_html/rox.php 在第 25 行

对 rox.php 进行编码

    <?php
          // use the IceCast class to obtain the stream details
          require_once('icecast.php');
          $oIceCast = new IceCast();

          // set server and mount
          $server = 'http://giss.tv:8000';
          $file   = '/status.xsl?mount=/radiosuguaFM.mp3';

          $oIceCast->setUrl($server,$file);
          $status = $oIceCast->getStatus();
          $result .= <<<EOF
    <div><strong>Nombre</strong> {$status['title']}</div>
    <div><strong>Descripcion</strong> {$status['description']}</div>
    <div><strong>Inicio de la transmision</strong> {$status['mount_start']}</div>
    <div><strong>Escuchas</strong> {$status['listeners']}</div>
    <div><strong>Max escuchas</strong> {$status['most_listeners']}</div>
    <div><strong>Url</strong> {$status['url']}</div>
    <div><strong>Artista actual</strong> {$status['now_playing']['artist']}</div>
    <div><strong>Track actual</strong> {$status['now_playing']['track']}</div>
    <audio controls>
    <source src="http://giss.tv:8000/radiosuguaFM.mp3">
    <!-- This is Where You Enter Your Flash Fall Back -->
    </audio>
EOF;
    ?>
    <!DOCTYPE html>
    <html lang="en">
    <head>
      <meta charset="utf-8" />
      <meta property="og:site_name" content="Script Tutorials" />
      <meta property="og:title" content="Script to read information from audio stream | Script Tutorials" />
      <meta property="og:image" content="http://www.script-tutorials.com/demos/398/thumb.png" />
      <meta property="og:type" content="website" />
      <meta name="description" content="Script to read information from audio stream - Script Tutorials">
      <title>Script to read information from audio stream | Script Tutorials</title>
      <link href="css/style.css" rel="stylesheet">
    </head>
    <body>
      <div class="container">
        <?= $result ?>
      </div><!--/.container-->
    </body>
    </html>
?>

此代码 de class icecast.php:

<?php
class IceCast {
  var $server = '';
  var $stats_file = "/status.xsl";
  var $radio_info = array();

  function __construct() {
    //build array to store our radio stats for later use        
    $this->radio_info['server'] = $this->server;
    $this->radio_info['title'] = 'Offline';
    $this->radio_info['description'] = 'Radio offline';
    $this->radio_info['content_type'] = '';
    $this->radio_info['mount_start'] = '';
    $this->radio_info['bit_rate'] = '';
    $this->radio_info['listeners'] = '';
    $this->radio_info['most_listeners'] = '';
    $this->radio_info['genre'] = '';
    $this->radio_info['url'] = '';
    $this->radio_info['now_playing'] = array();
    $this->radio_info['now_playing']['artist'] = 'Unknown';
    $this->radio_info['now_playing']['track'] = 'Unknown';
  }

    function setUrl($url,$file) {
        $this->server=$url;
        $this->stats_file=$file;
        $this->radio_info['server'] = $this->server;
    }

  private function fetch() {
    //create a new curl resource
    $ch = curl_init();

    //set url
    curl_setopt($ch,CURLOPT_URL, $this->server . $this->stats_file);

    //return as a string
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

    //$output = our stauts.xsl file
    $output = curl_exec($ch);

    //close curl resource to free up system resources
    curl_close($ch);

    return $output;
  }

  function getStatus() {
    $output = $this->fetch();

    //loop through $ouput and sort into our different arrays
    $temp_array = array();

    $search_for = "<td\s[^>]*class=\"streamdata\">(.*)<\/td>";
    $search_td = array('<td class="streamdata">', '</td>');


    if (preg_match_all("/$search_for/siU", $output, $matches)) {
      foreach ($matches[0] as $match) {
        $to_push = str_replace($search_td, '', $match);
        $to_push = trim($to_push);
        array_push($temp_array, $to_push);
      }
    }

    if (count($temp_array)) {
      //sort our temp array into our ral array
      $this->radio_info['title'] = $temp_array[0];
      $this->radio_info['description'] = $temp_array[1];
      $this->radio_info['content_type'] = $temp_array[2];
      $this->radio_info['mount_start'] = $temp_array[3];
      $this->radio_info['bit_rate'] = $temp_array[4];
      $this->radio_info['listeners'] = $temp_array[5];
      $this->radio_info['most_listeners'] = $temp_array[6];
      $this->radio_info['genre'] = $temp_array[7];
      $this->radio_info['url'] = $temp_array[8];

      if (isset($temp_array[9])) {
        $x = explode(" - ", $temp_array[9]);
        $this->radio_info['now_playing']['artist'] = $x[0];
        $this->radio_info['now_playing']['track'] = $x[1];
      }
    }
    return $this->radio_info;
  }
}
?>

非常感谢您的帮助

【问题讨论】:

  • 第 25 行是哪一行?
  • 旁注:EOF; 包含从左侧到 E 开头的 4 个空格,至少在您的问题中。如果它们在您的实际工作代码中,请将它们删除。

标签: php arrays variables curl global-variables


【解决方案1】:

先检查是否定义了再使用

我认为因为这个$result .= &lt;&lt;&lt;EOF 你得到了错误

require_once('icecast.php');
$oIceCast = new IceCast();
.....
......

if(empty($result))
{
$result = '';
}
.....
.....

$result .= <<<EOF.....
.......
.......

【讨论】:

  • 我在脚本中添加了以下内容: if(empty($result)) { $result = 'Not Variable'; } 并打印 Not Variable
【解决方案2】:

您正在尝试追加到现有变量:

$result .= <<<EOF

$result实际上并不存在于此。
您只是想创建和分配变量:

$result = <<<EOF

【讨论】:

  • 我按照您的要求删除了“。”我在脚本中添加了以下内容:if(empty($result)) { $result = 'Not Variable'; } 并打印 Nombre Offline 描述 Radio offline Inicio de la transmision Escuchas Max escuchas Url Artista actual Unknown Track actual Unknown and my localhost print Nombre Radio Sugua Descripcion Radio comunitaria garifuna de sambo creek Inicio de la传输 Fri, 20 Jun 2014 21:41:38 +0200 Escuchas 1 Max escuchas 5 Url Artista actual Track actual
猜你喜欢
  • 1970-01-01
  • 2016-12-03
  • 1970-01-01
  • 2023-03-03
  • 2017-07-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-12
相关资源
最近更新 更多