0

我有一个关于在 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 部分来自数据库。

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

4

1 回答 1

1

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

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

class RadInfo {

    private $db;
    private $cfg;

    private $dnas_data;

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


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

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

你真的openstats()要再打电话吗?将此更改为:

return $this->dnas_data['SERVERTITLE'];
于 2013-09-10T15:06:58.790 回答