Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

get_variables and set_variables isseue in phpagi with asterisk 18.22 #33

Open
Hishamjan opened this issue Jul 9, 2024 · 1 comment
Open

Comments

@Hishamjan
Copy link

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
Copy link

fatburger25 commented Oct 16, 2024

// 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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants