welltime / phpagi

PHPAGI is a PHP class for the Asterisk Gateway Interface. The package is available for use and distribution under the terms of the GNU Public License.
GNU Lesser General Public License v2.1
58 stars 93 forks source link

get_variables and set_variables isseue in phpagi with asterisk 18.22 #33

Open Hishamjan opened 4 months ago

Hishamjan commented 4 months ago

Hello, I am usign phpagi 2.20 with php 8 and asterisk 18.22 when set and get variables run in the agi script, the results show that theya re set with some value, but upon checking its data part of the array, it is usually empty/null..

Due to this, i am unable to push data in my database tables...

fatburger25 commented 1 month ago
// evalaute function
function evaluate($command)
{
    $broken = array('code'=>500, 'result'=>-1, 'data'=>'');

    // write command
    if(!@fwrite($this->out, trim($command) . "\n")) {
        $this->conlog("Failed to write command: $command");
        return $broken;
    }
    fflush($this->out);
    $this->conlog("Sent command: $command");

    // Read result
    $response = '';
    $count = 0;
    while ($count < 10) { // Increase timeout, max 10 iterations
        $line = fgets($this->in, 4096);
        $this->conlog("Raw read: " . trim($line));
        if ($line === false) {
            $this->conlog("fgets returned false, possible EOF");
            break;
        }
        $response .= $line;
        if (substr(trim($line), 0, 3) === '200') {
            break; // We got a complete response
        }
        $count++;
    }

    if ($count >= 10) {
        $this->conlog("Timeout waiting for response to: $command");
        return $broken;
    }

    // parse result
    $parsed = $this->parse_agi_response($response);
    $this->conlog("Parsed response: " . print_r($parsed, true));

    return $parsed;
}

function parse_agi_response($response)
{
    $parsed = array('code'=>500, 'result'=>-1, 'data'=>'');

    $lines = explode("\n", trim($response));
    foreach ($lines as $line) {
        $line = trim($line);
        if (preg_match('/^(\d{3}) (.*)/', $line, $matches)) {
            $parsed['code'] = intval($matches[1]);
            $parts = explode(' ', $matches[2], 2);
            $parsed['result'] = $parts[0];
            $parsed['data'] = isset($parts[1]) ? trim($parts[1], '()') : '';
            break;
        }
    }

    return $parsed;
}

Please replace the existing evaluate function from the phpagi.php and add the new function parse_agi_response,

this should help you with the issue. @Hishamjan @alexeevdv @asolovjov @Arbuzov