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

时间:2013-09-10 15:03:55

标签: php curl

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

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

1 个答案:

答案 0 :(得分:1)

您在方法中声明$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'];