Need some PHP help for this script

  • I am not now, and have never claimed to be, a PHP developer.

    What I am wanting to do is create a webpage to drop on my FreePBX system that will let me reboot or reload Yealink phones.

    I have worked out the backend connectivity. Now I jsut need something to parse the output and create the buttons that will send the commands.

    I started this in PHP because that is what the examples I previously had from another script as well as the new stuff I found all used. I absolutely do not care about it remaining in PHP.

    The script will connect to Asterisk and get a list of all the extensions with the command pjsip list endpoints. I have this done.

    The page now needs to parse the result into an array of extensions. I guess a little logic can be used to determine what is really an extension. Here is what is returned. In this example, only the 101 - 110 are valid. At another site it would be 5100 - 5499, and yet another it would be 200-299.

    I envision this dumping into a list view with a checkbox to select the ones I want to reload or reboot and then two buttons labeled Reload and Reboot respectively.

    The page will then parse for the selected extensions and send the appropriate action based on the button clicked. Potentially an "are you sure" warning.

    The action to send is a simple string with all the extensions tacked on the end space delimited.
    Such as pjsip send notify reload-yealink endpoints 101 102 103 110 or pjsip send notify reboot-yealink endpoints 101 102 103 110

    The code in it's current form is on my github if anyone wants to work on it that way.

    // Some initial sample code found at
    // amportal.conf code modified from
        // Open /etc/amportal.conf and get the Asterisk Manager connection information
        define("AMP_CONF", "/etc/amportal.conf");
        $file = file(AMP_CONF);
        if (is_array($file)) {
            foreach ($file as $line) {
                if (preg_match("/^\s*([a-zA-Z0-9_]+)=([a-zA-Z0-9 .&[email protected]=_!<>\"\']+)\s*$/",$line,$matches)) {
                    $amp_conf[ $matches[1] ] = $matches[2];
        require_once('DB.php'); //php-pear-db must first be installed on a new FreePBX 14 system
        $db_user = $amp_conf["AMPMGRUSER"];
        $db_pass = $amp_conf["AMPMGRPASS"];
        $db_host = $amp_conf["ASTMANAGERHOST"];
        $db_port = $amp_conf["ASTMANAGERPORT"];
        $db_timeout = $amp_conf["ASTMGRWRITETIMEOUT"];
        // Connect to Asterisk Manager and run a command
        $socket = fsockopen($db_host, $db_port, $errno, $errstr, $db_timeout);
        fputs($socket, "Action: Login\r\n");
        fputs($socket, "UserName: $db_user\r\n");
        fputs($socket, "Secret: $db_pass\r\n\r\n");
        fputs($socket, "Action: Command\r\n");
        fputs($socket, "Command: pjsip list endpoints\r\n\r\n");
        fputs($socket, "Action: Logoff\r\n\r\n");
        while (!feof($socket)) {
            $wrets .= fread($socket, 8192);

  • I'm not a PHP dev either, but I've been slowly learning it and this seems like a fun challenge to help with that process.

    So, if you don't mind... I'm going to take a stab at it as I have time... or at least until someone else whose more experienced provides a working solution first.

  • It's hard to work without having data to use. Could you copy all that data from your screenshot to a text file so I can copy / paste that to use?

  • @obsolesce said in Need some PHP help for this script:

    It's hard to work without having data to use. Could you copy all that data from your screenshot to a text file so I can copy / paste that to use?

    Sorry I went offline after that to play a game of Life with the kids.

  • For anyone interested. Thanks to @Obsolesce this is progressing nicely.

    Current code is on my github.

    Have extensions being parsed into an array. Now just to wrap things into a pretty form.


  • I added AstMan.php to github and it contains all the connection stuff wrapped in functions with error handling. But I am having a hell of a time trying to use them.

    @dafyre .........

  • @JaredBusch -- Check my pull request.

  • @dafyre said in Need some PHP help for this script:

    @JaredBusch -- Check my pull request.

    That worked, minus a syntax correction.

  • Added in @Obsolesce's last commit for the form.

    Now to make the form actually submit.

  • next bit. I have this being returned by a function.

    string(3771) "Asterisk Call Manager/4.0.3
    Response: Success
    Message: Authentication accepted
    Event: FullyBooted
    Privilege: system,all
    Uptime: 151132
    LastReload: 40568
    Status: Fully Booted
    Response: Success
    EventList: start
    Message: Following are Events for each object associated with the Endpoint
    Event: EndpointDetail
    <snip not needed>
    Event: AuthDetail
    <snip not needed>
    Event: AorDetail
    <snip not needed>
    Event: ContactStatusDetail
    AOR: 5120
    URI: sip:[email protected]:5060
    UserAgent: Yealink SIP-T42G
    RegExpire: 1532368885
    CallID: [email protected]
    Status: Reachable
    RoundtripUsec: 15857
    EndpointName: 5120
    ID: 5120;@9e6900eb84ddc2a05701b109000ae8e5
    AuthenticateQualify: 0
    QualifyFrequency: 60
    QualifyTimeout: 3.000
    Event: EndpointDetailComplete
    EventList: Complete
    ListItems: 4
    Response: Goodbye
    Message: Thanks for all the fish.

    I do not need the Event responses except for ContactStatusDetail.
    I want to get that section dropped into an array in PHP.

    if I am somehow getting each line of text in the variable $line, I can use explode I believe?

    $split =  explode(":",$line,2);

    I would then need to add each line to an array?

  • Frustration and finally progress. Buttons do nothing yet, but @Obsolesce has been working on that in his spare time.


    Currently requires 2 files from my github. AstMan.php and yealink.reload.php

  • @jaredbusch said in Need some PHP help for this script:


    What you're building looks a little like what you have in 3CX. They also show ip address, mac address and the user name and some other minor stuff. As options besides reload (called reprovision) and reboot, they also "firmware update" and also a direct link to the phones webserver.
    Maybe some of that would make sense to add to your php script?

    PS. Checking the input from the form is relatively easy. Make sure the form tag in the generated html uses post <form method="post"> . Then when you receive the values into the php script you can do print_r($_POST); and you'll see how everything looks. Then it's just a matter of going through the $_POST array and picking out the extensions you selected.

    It's the name of the checkboxes in html that will appear in the $_POST array. So when the html table is generated use the extension number as name for the checkboxes, for instance ext_101, ext_102 etc.

  • This is now working at a basic level.

    Code on GitHub.

  • github updated. files renamed a bit. Still a work in progress, but things working much better.