简体   繁体   中英

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

I have a small question about running a script on windows ( IIS 8 + PHP 5.4 ) I try to make the script below to work.

<?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'];
    }


}

?>

I have made changes to the script but the output stays on this moment NULL.

I have tried to call the class/function thought:

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

and

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

I have tried several options but noting work. I had first problems with cURL but that is solved because PHP fail to load the module.

Now i have problems getting any information. The LoadServerTitle gives a output that says NULL but i cant get it to work properly. The $this->cfg part comes out the database.

Can someone give me a idea to get this script working. I want to thank the people that help me so far.

You declare $dnas_data in a method, so it won't be accessible outside that method.

I would suggest you to declare another member variable for storing the data:

class RadInfo {

    private $db;
    private $cfg;

    private $dnas_data;

And replace every occurrence of $dnas_data by $this->dnas_data


I just looked again at your GetServerTitle() method:

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

Do you really want to call openstats() again? Change this to :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM