未定义的变量,通过在 PHP 5.4 和 IIS 8 上运行脚本


Undefined Variable by running a script on PHP 5.4 and IIS 8

我有一个关于在Windows上运行脚本的小问题(IIS 8 + PHP 5.4)我尝试使下面的脚本正常工作。

<?php
class RadInfo {
    private $db;
    private $cfg;
    private $dnas_data;
    function __construct() {
        $this->db = Com_DB::getInstance();
        $this->cfg = new Config();
    }
    function openstats() {
        $ch = curl_init($this->cfg->shoutcasthost . '/admin.cgi?sid=' . $this->cfg->shoutcastsid . '&mode=viewxml');
        curl_setopt($ch, CURLOPT_PORT, $this->cfg->shoutcastport);
        curl_setopt($ch, CURLOPT_USERAGENT, $this->cfg->shoutcastuagent);
        curl_setopt($ch, CURLOPT_TIMEOUT, 5);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
        curl_setopt($ch, CURLOPT_USERPWD, $this->cfg->shoutcastuser . ':' . $this->cfg->shoutcastpasswd);
        $curl = curl_exec($ch);
        curl_close($ch);
        if ($curl) {
            $xml = @simplexml_load_string($curl);
            $dnas_data = array (
                'CURRENTLISTENERS'    => $xml->CURRENTLISTENERS,
                'PEAKLISTENERS'        => $xml->PEAKLISTENERS,
                'MAXLISTENERS'        => $xml->MAXLISTENERS,
                'REPORTEDLISTENERS'    => $xml->REPORTEDLISTENERS,
                'AVERAGETIME'        => $xml->AVERAGETIME,
                'SERVERGENRE'        => $xml->SERVERGENRE,
                'SERVERURL'            => $xml->SERVERURL,
                'SERVERTITLE'        => $xml->SERVERTITLE,
                'SONGTITLE'            => $xml->SONGTITLE,
                'NEXTTITLE'            => $xml->NEXTTITLE,
                'SONGURL'            => $xml->SONGURL,
                'IRC'                => $xml->IRC,
                'ICQ'                => $xml->ICQ,
                'AIM'                => $xml->AIM,
                'STREAMHITS'        => $xml->STREAMHITS,
                'STREAMSTATUS'        => $xml->STREAMSTATUS,
                'BITRATE'            => $xml->BITRATE,
                'CONTENT'            => $xml->CONTENT,
                'VERSION'            => $xml->VERSION,
            );
            if ($dnas_data['STREAMSTATUS'] == 1)
            {
                foreach ($xml->LISTENERS->LISTENER as $listener)
                {
                    $sc_data['LISTENERS'][] = array(
                        'HOSTNAME' =>  $listener->HOSTNAME,
                        'USERAGENT' =>  $listener->USERAGENT,
                        'CONNECTTIME' =>  $listener->CONNECTTIME,
                        'POINTER' =>  $listener->POINTER,
                        'UID' =>  $listener->UID,
                    );
                }
                foreach ($xml->SONGHISTORY->SONG as $song)
                {
                    $sc_data['SONGHISTORY'][] = array(
                        'PLAYEDAT' =>  $song->PLAYEDAT,
                        'TITLE' =>  $song->TITLE,
                    );
                }
            }
        } else {
            $dnas_data = array('ERROR' => 'Could not connect to dnas-server!');
        }

    }

    function LoadServerTitle() {
            return $this->dnas_data['STREAMTITLE'];
    }

}
?>

我已经对脚本进行了更改,但输出此刻仍为 NULL。

我试图调用类/函数思想:

$comcms->slim->get('/streamtitle', function() use($comcms) {
        $comcms->jsonOutput($comcms->radinfo->LoadServerTitle());
}); 

$comcms->slim->get('/streamtitle', function() use($comcms) {
    if ($comcms->radinfo->openstats()) {
        $comcms->jsonOutput($comcms->radinfo->LoadServerTitle());
    }
});

我已经尝试了几个选项,但注意到工作。我在使用 cURL 时遇到了第一个问题,但这已经解决了,因为 PHP 无法加载模块。

现在我在获取任何信息时遇到问题。LoadServerTitle给出了一个输出,上面写着NULL,但我无法让它正常工作。$this->cfg 部分来自数据库。

有人可以给我一个想法来让这个脚本工作。我要感谢到目前为止帮助过我的人。

您在方法中声明$dnas_data,因此在该方法之外无法访问它。

我建议您声明另一个成员变量来存储数据:

class RadInfo {
    private $db;
    private $cfg;
    private $dnas_data;

并将每次出现的$dnas_data替换为$this->dnas_data

<小时 />

我刚刚又看了一遍你的GetServerTitle()方法:

return($this->openstats($dnas_data['SERVERTITLE']));

你真的想再给openstats()打电话吗?将其更改为 :

return $this->dnas_data['SERVERTITLE'];